Skip to content

Branching

benoitbleuze edited this page Aug 25, 2012 · 9 revisions

Here we enter the real deal. Easy costless branching is the best advantage Git brings to the table, compared to old CVS like SCMs where creating a branch was overly complicated, or even svn where it takes forever to create all the necessary hard links on a large project.

Branches are two things:

  • bookmarks to a commit. When a branch is created it points at a commit as its branching point
  • New paths for new children in the history tree. All new commits done in the new branch will be parented by this new commit, diverging from the previous branch the user was in.

The ideas behind branches are very graphical. Any time you feel lost in your branches and which commit goes where and when, run gitk, display the branches, or take a pen and retrace your history by drawing lines. This is true for all branches, and even more valuable when working with branches on different remote repositories, but that is another story for later on...

Create a branch

In fact we already created one. It is called master. But let's create a new one:

ben@GregoryHouse:~/tmp/testRepo (master)$ git branch experimental
ben@GregoryHouse:~/tmp/testRepo (master)$ git branch -v
  experimental c6c5566 Add .gitignore, add a line to README
* master       c6c5566 Add .gitignore, add a line to README

We created a branch that points at the last commit on master. They are identical so far. but any commit from now on on master will only be present in master , and vice versa.

The git branch command and its options (once again git branch --help is your best friend) allows you to manage your branches: add, rename, delete, list local remote branches, display the head of there tree, are all the possible actions there.

Jump to Switch Branches


Only look here if you have time and are curious about the Life the Universe and Everything. We could also have created a branch from any existing commit:

ben@GregoryHouse:~/tmp/testRepo (master)$ git log --oneline
c6c5566 Add .gitignore, add a line to README
4a9deba First commit: add README
ben@GregoryHouse:~/tmp/testRepo (master)$ git checkout 4a9deba
Note: checking out '4a9deba'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 4a9deba... First commit: add README
ben@GregoryHouse:~/tmp/testRepo ((4a9deba...))$ git branch alternateHistory
ben@GregoryHouse:~/tmp/testRepo ((4a9deba...))$ git branch -v
* (no branch)      4a9deba First commit: add README
  alternateHistory 4a9deba First commit: add README
  experimental     c6c5566 Add .gitignore, add a line to README
  master           c6c5566 Add .gitignore, add a line to README

Several interesting things happened here:

  • We went back in time by checking out an old commit. So we were not in a branch any more. Git warned us about being in detached HEAD state. It means as seen previously, that no commit can be done from there (you may try, it won't hurt).
  • We created a branch based on a different commit than the current head in master. The interest of doing this is for instance to try to rewrite changes made previously, but without being sure it is worth it, and to be safe, you still keep the current master branch in its current state while you experiment our changes in an other topic branch.
  • we are still in that (no branch) state, and we will need to checkout a branch to be able to commit...
  • Finally git fives us the following advice git checkout -b new_branch_name to keep our commits. We will see what it means in the next section.

Clone this wiki locally