When running commands like git status or git commit, you might have noticed the text "On branch master" crop up repeatedly.

The basic idea of branching is that rather than a single stream of history, a repository can contain many, and different streams of history can be combined:

Although the specifics vary, modern version control systems generally support the concept of branches. In Git, branches are cheap, fast, and (usually) easy to work with, which has quickly made them an indispensable tool.

Once upon a time, when a programmer wanted to experiment with a different version of a program - for example, by adding a new feature - the safest thing was to make a copy of the files and work on the copy until it seemed to be good. Which is part of how you wind up with this scenario:

In Git, instead of making a direct copy of the files, you can say "I would like the history to fork here", like so:

git branch alternate_universe
git checkout alternate_universe

...and then you can go on your merry way, changing files and making commits like usual, and none of them will affect the other branches until (and only if) you're ready to combine them, which you can do by saying:

git checkout master
git merge alternate_universe

Nothing says you have to merge any branches. If the changes I'm trying to make don't work out, I often simply discard the branch I've started like so:

git branch -d alternate_universe

...where -d is for "delete".

This guide was first published on Jul 15, 2015. It was last updated on Jul 15, 2015.

This page (Branches?) was last updated on Mar 05, 2021.

Text editor powered by tinymce.