Tutorial on Creating a branch with git

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

Following from our previous tutorial, we now create a branch call “bugfix”.  Before branching it is nice to keep things in a clean state first.

1.  Then type …

git branch bugfix

That created the branch.

2.  Now switch to that branch…

git checkout bugfix

A short cut that would create the branch and switch to it in one command would be …

git checkout -b bugfix

3. Work on the bug by altering the file index.html.

4. Commit the change.

git add .
git commit -m “bug fixed”

5. Launch gitk and you can see visually how the branch bugfix and master diverge.

6.  If you were to switch to master now with …

git checkout master

and look in the file, the bug fix would not be there.  That is because the bug fix was in the branch bugfix.

7. To merge that fix into master.  We type …

git merge bugfix

while we are on the master branch.

8.  Now the bug fix is in the master branch as well.  If you don’t need the branch anymore, you can delete the branch by …

git branch -d bugfix

It will say “deleted branch bugfix” and you can confirm with gitk.

You can learn more detail about branching in the git book.