Git - Branching and Merging
<slideshow style="nobleprog" headingmark="⌘" incmark="…" scaled="true" font="Trebuchet MS" >
- title
 - Git for Users Training Course
 - author
 - Bernard Szlachta (NobleProg Ltd)
 
</slideshow>
Branching and Merging ⌘
git branch
list all branches
git branch <branch>
create a new branch named <branch>, referencing the same point in history as the current branch
git branch <branch> <start-point>
create a new branch named <branch>, referencing <start-point> (branch, tag or commit)
git branch -d <branch>
delete the branch <branch>; if the branch you are deleting points to a commit which is not reachable from the current branch, this command will fail with a warning.
git branch -D <branch>
- forces to do even branch points to a commit not reachable from the current branch
 - failed experiment branches
 - use only for changes in your repository
 
git checkout <branch>
switch to the <branch>
git checkout -b <new> <start-point>
create a new branch <new> referencing <start-point>, and check it out
Dealing with Branches and Tags ⌘
 git clone git://git.kernel.org/pub/scm/git/git.git
 git branch
 git branch -r
 git tag
 git checkout maint
 git branch
 git checkout v1.7.4
 git branch
Creating Branches ⌘
cp repo/ repo.bak -R; cp central/ central.bak -R
Create new branch and switch working copy
git branch experiment git checkout experiment git checkout -b experiment
Merging ⌘
- switch to experiment
 - modify the afile.txt
 
git commit -a git checkout master modify afile.txt (different part) git commit -a gitk --all git branch git merge experiment gitk --all
Exercise (REmoving) ⌘
- Create branch v2.x
 - Modify a couple of files in the branch
 - Merge the branch with master
 - Remove the branch v2.x
 
Exercises (remote branching) ⌘
- Create a branch “quickfix” from a master branch
 
git checkout master git checkout -b quickfix
- create quickfix.txt in quickfix branch and commit
 
touch quickfix.txt git add quickfix.txt git commit -m "quickfix"
- switch to master
 
git checkout master
- Commit 2 changes into the master branch
 
echo 1 >> afile.txt; git commit -a -m "change1" echo 2 >> afile.txt; git commit -a -m "change2"
- switch back to quickfix
 
git checkout quickfix
- analyse situation in
 
gitk --all
- Push the quickfix to the production
 
git push origin quickfix
- On production
 
git fetch origin quickfix git branch -a git checkout origin/quickfix -b quickfix
- On Devel
 
merge quickfix into masterbranch
- delete quickfix branch
 
Reverting Merges ⌘
Adding commit with reverted changes
git revert
Before Committing
git reset --hard HEAD
After Committing
git reset --hard ORIG_HEAD
use only if you committed it only to your private repo
Rebasing ⌘
git checkout -b mywork origin echo mywork changes >> afile.txt git commit -a git checkout master echo some new func >> afile_master.txt git add afile_master.txt git commit gitk --all git rebase mywork gitk --all git prune gitk --all
Exercise ⌘
- Create a branch “quickfix”
 - add a couple of changes afile.txt in quickfix branch
 - switch to master
 - Commit 2 changes into the master branch
 - switch back to quickfix
 - analyse situation in gitk --all
 - rebase quickfix branch to master
 - analyse situation in gitk -all
 - prune dangling objects
 - gitk --all
 
Cherry Picking ⌘
Stashing ⌘
- git-stash to save the current state of your work
 - git stash save "work in progress for foo feature"
 - echo “bug fix” >> afile.txt
 - git commit -a -m "fixed the bug"
 - git stash pop
 
Workflow Structure ⌘
Combining Commits ⌘
- Version upto 1.6
 
git format-patch v1.0..HEAD git reset v1.0 git apply *.patch git commit
- Version 1.7 onwords
 
git reset v1.0 git commit
