Visualizing a commit tree what is a branch? Lightweight, movable pointer to a branch in a repository. A branch is a copy of a snapshot of a project's source code in a working directory. Gitignore ignores files in your working directory that are not tracked. Ignore files in the build / directory build / but not doc / server / arch.txt. Ignore.a files in the directory doc
Visualizing a commit tree what is a branch? Lightweight, movable pointer to a branch in a repository. A branch is a copy of a snapshot of a project's source code in a working directory. Gitignore ignores files in your working directory that are not tracked. Ignore files in the build / directory build / but not doc / server / arch.txt. Ignore.a files in the directory doc
Visualizing a commit tree what is a branch? Lightweight, movable pointer to a branch in a repository. A branch is a copy of a snapshot of a project's source code in a working directory. Gitignore ignores files in your working directory that are not tracked. Ignore files in the build / directory build / but not doc / server / arch.txt. Ignore.a files in the directory doc
Determines file state .gitignore Example # a comment - this is ignored # no .a files *.a # but do track lib.a, even though you're ignoring .a files above !lib.a # only ignore the root TODO file, not subdir/TODO /TODO # ignore all files in the build/ directory build/ # ignore doc/notes.txt, but not doc/server/arch.txt doc/*.txt # ignore all .txt files in the doc/ directory doc/**/*.txt DEMO Repositories What is a Branch? Visualizing a Commit tree blob 5b1d3 README blob 911e7 LICENSE blob cba0a test.rb blob == Testing Library
This library is used to test Ruby projects. blob The MIT License Copyright <year> <co
Permission is hereby gran blob require rubygems require pp
module Test
commit initial commit of my project What is a commit? Commit object Pointer to the snapshot Author, committer, and message Zero, or more, pointers to parent commit(s)
Tree object Project directory checksum File version hash and filename
Blob Versioned file Checksummed Multiple Commits commit initial commit of my project commit fixed bug #1328 stack overflow under certain commit add feature #32 ability to add new formats to the Now, whats a branch? Lightweight, movable pointer to a commit Default branch is master Every commit moves the pointer forward What about creating a branch? $ git branch testing Creates a new pointer to same commit What branch are we on? Special pointer called HEAD Pointer to local branch HEAD Switching branches $ git checkout testing HEAD is now pointing at testing HEAD Now what? Lets commit on the testing branch $ git commit HEAD Checkout master $ git checkout master This did two things Moved the HEAD pointer Reverted files to snapshot master points at HEAD Commit to master History has diverged $ git commit
HEAD Things to remember A branch in Git is A simple file Contains a 40 character SHA-1 checksum of the commit Cheap to create and destroy Creating a branch is merely writing 41 bytes to a file
DEMO Branching Basics Basic Branching Heres a scenario 1. Do some work for a web site 2. Create a branch for a new story 3. Do some work on that branch Now, you get a call that there is a critical issue that needs a hotfix in production 1. Create a branch to add the hotfix 2. Commit the hotfix 3. After its tested, merge the hotfix branch, & push to production 4. Switch back to original story and continue 1. Do some work for a web site We already have a few commits 2. Create a branch for a new story Create & checkout the story branch $ git branch story $ git checkout story 3. Do some work on the story branch Do modifications Commit those changes Now we get a call Need to fix immediately Switch back to master $ git checkout master 1. Create a hotfix branch Create & checkout hotfix $ git checkout -b hotfix 2. Commit the hotfix Fix the issue Commit the issue 3. After testing, merge hotfix to master $ git checkout master $ git merge hotfix 4. Checkout story, and continue work Delete hotfix branch first $ git branch -d hotfix DEMO Hotfix Scenario Basic Merging Time to merge the story Commit isnt directly upstream Fast-forward is not possible Checkout master, merge story
$ git checkout master $ git merge story Three-way merge Common Ancestor Snapshot to Merge Into Snapshot to Merge In Merge results Creates a merge commit A commit with more than one parent Story can be deleted Merge results After story is deleted Basic merge conflicts Pauses the merge commit Git status shows unmerged conflicts Resolve & Commit Manually resolve and git add Or, git mergetool git status git commit Conflict What does a conflict look like?
<<<<<<< HEAD:index.html <div id="footer">contact : clientservices@nwcadence.com</div> ======= <div id="footer"> please contact us at services@nwcadence.com </div> >>>>>>> story:index.html
Resolved Conflict Using both HEAD and story <div id="footer"> please contact us at clientservices@nwcadence.com </div>
Use git add and then git commit DEMO Merge Conflict Remote Branches Remote Branch Ref. to branch on remote repo Local branches that cant be moved Auto-moved on a fetch Take the form of: (remote)/(branch) Example: origin/master Local branch can track remote branch Local: master Remote: origin/master Cloning a repo http://git.nwc.com/_git/project My Computer Histories move separately Someone pushes two commits http://git.nwc.com/_git/project Two new commits Histories move separately I commit twice My Computer I commit twice Histories move separately git fetch origin My Computer Multiple remotes We have a team repository Only used for development http://git.nwc.com/_git/project http://git.team1.nwc.com/_git/project Incorporate team1s repo Add and fetch git remote add team1 http://git.team1.nwc.com/_git/project git fetch team1
My Computer Pushing a branch We created a branch goodidea We want to share it We need to push it git push (remote) (branch) goodidea git push Tracking Branches Direct relationship between local and remote git push knows where to go git pull automatically merges Clone only tracks master Track other remotes and branches git checkout -b [branch] [remotename]/[branch] DEMO Remote Branches Rebasing Rebase Another integration method How it works: Going to the common ancestor Getting the diff introduced by each commit Save those diffs to temp files Reset current branch to rebase branch Apply each diff to the branch Visualize a rebase Heres an example Visualize a rebase First, lets merge Merge experiment into master Visualize a rebase Now, lets rebase Rebase experiment onto master Why rebase instead of merge? No difference in the end Snapshot will be the same Merge & Rebase commit is exactly the same Linear history Work appears to be in series Cleanly apply to remote branch Less integration work just a fast-forward The golden rebase rule Do not rebase commits that you have pushed to a public repository Scenario: Interesting rebase Scenario: Interesting rebase We want client code Want to wait before bringing in server $ git rebase --onto master server client Scenario: Interesting rebase Lets merge client onto master Only a fast-forward here $ git checkout master $ git merge client Scenario: Interesting rebase Now, rebase server onto master Lets do it without checking out server git rebase [basebranch] [topicbranch] $ git rebase master server Scenario: Interesting rebase Merge server into master Delete server and client branches Remember
Do not rebase commits that you have pushed to a public repository