What is Branching?

In git, a branch is a copy of all the files in your codebase. Each branch has an identifying name and its own set of version or commit history. When you create a new repository, the default branch is called master. Even if you do not create any additional branches, you'll be performing all git commands on a branch called master.

Why Would I Want to Branch?

Branching is a powerful mechanism in any SCM tool that allows developers to veer off into a tangential or experimental direction, without affecting the main codebase.

For example, suppose you want to experiment with a new feature, but this feature requires touching a huge amount of code in your codebase. You're not sure if this feature will work, and you want to play around without it affecting your main codebase. You can create a new branch to perform this work, and throw it all away if it doesn't work out.

To create a branch, we fork it from another existing branch, likely master. Then we switch to the newly forked branch and start working inside the new branch. The new experimental branch will contain all the historical changes in the master branch up to the point of the fork. That is, the two branches do not stay in sync automatically going forward (this is good, because we are experimenting in our new branch). If we decide to discard that experiment, we can simply switch back to the master branch and delete the experimental branch. On the other hand, if we decide the experiment is a success then we may want to keep the code. If that is the case, we can merge commits from the experimental branch into our master branch (and then delete the experimental branch, if we want to).

It's important to note that only commits, not staged files, are forked and merged. In git, we only move commits around.

Local vs. Remote

It is important that you understand branches can exist both locally and on remote repositories (we'll get into remote repositories later). You can setup your local branches to track remote branches, which means they will automatically push and pull code to the tracked branch on the remote repository. While tracking a branch is possible, it is not required. We'll talk more about this when we talk about remote repositories.


You may have noticed we didn't go over any commands on branching. For now, we want you to understand the concept of branches in git, and not worry about using them. Git can get complicated pretty quickly, so it's important to understand the concepts first.

If you're comfortable with git branching, try to create a few local branches and make a few changes in the new branch. See how your local file system changes as you switch between branches.