Git - Branching and Merging

From Training Material
Jump to navigation Jump to search


title
Git for Users Training Course
author
Bernard Szlachta (NobleProg Ltd)

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) ⌘

  1. Create branch v2.x
  2. Modify a couple of files in the branch
  3. Merge the branch with master
  4. 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-rebasing1.png Git-rebasing2.png Git-rebasing3.png

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 ⌘

  1. Create a branch “quickfix”
  2. add a couple of changes afile.txt in quickfix branch
  3. switch to master
  4. Commit 2 changes into the master branch
  5. switch back to quickfix
  6. analyse situation in gitk --all
  7. rebase quickfix branch to master
  8. analyse situation in gitk -all
  9. prune dangling objects
  10. gitk --all


Cherry Picking ⌘

Git-cherry-picking.jpgGit-cherry.png
touch afile.txt
git add afile.txt
git commit -m "new filo"
git checkout -b exp
echo Not Cherry1 >> afile.txt 
git commit -a -m NotCherry1
echo Cherry >> afile.txt 
git commit -a -m Cherry
echo Not Cherry2 >> afile.txt 
git commit -a -m NotCherry2
git checkout master
echo masterchange >> afile_master.txt 
git add afile_master.txt
git commit -a -m "Master Change"
git log exp
git cherry-pick 3c8f

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 ⌘

Git-wf-structure.png


Combining Commits ⌘

  1. Version upto 1.6
git format-patch  v1.0..HEAD
git reset v1.0
git apply *.patch
git commit
  1. Version 1.7 onwords
 git reset v1.0
 git commit