How to Create a New Branch in Git

Branches are central to the concept of version control in programming, and Git in particular. This starter article tells you what a branch is and how to create one using a number of different tools.

What is a Git Branch?

In version control systems, the term branch is used as an analogy with trees in the sense that each branch emerges from another, eventually ending up back at the trunk. Branches allow you to create individual lines of development, in order to work on them in isolation without disturbing other work.

Related: How to Use Git Branches to Structure Your Programming Project

Using Git, you’ll be working on the master branch by default, whether you’re aware of it or not. This is often referred to as your active, current, checked-out, or HEAD branch. At any time during your development cycle, you can create a new branch and carry out separate work in each branch, from that point onwards.

Creating a New Branch on the Command Line

The command-line Git program offers the most power and flexibility, but there’s a lot to learn. If you’re comfortable digging around the man pages and make heavy use of Git, it’s a great option.

Use the git branch <branchname> command to create a new branch with the given name:

$ git branch dev
Branch 'dev' set up to track local branch 'master'.

This branches from the current branch, so make sure you’ve switched to the one you want to branch from before you execute that command.

You can list all branches and confirm the new one has been created using git branch without any arguments:

$ git branch
1 dev
2 * master

You can see more information, including which branch another one tracks, using the -vv flag:

$ git branch -vv
1 dev d1a9e5b [master] commit comment
2 * master d1a9e5b commit comment

If you try to create a branch before the first commit, you’ll get an error message like:

fatal: Not a valid object name: 'master'.

If you try to create a branch using a name that already exists, you’ll get an error message like:

fatal: A branch named 'dev' already exists.

The git branch command creates a new branch pointing to the same commit you’re currently working on. However, your working copy will still be pointing at the master branch. To switch to the new branch you just created, use git checkout:

git checkout dev

The term checkout might be confusing if you’re used to other version control systems; in Git, checkout refers to switching the currently active branch. Since you’ll usually want to switch to a new branch once it’s created, there’s a shortcut for the whole process:

git checkout -b dev

That command means “create a new branch called ‘dev’ and switch to it immediately”. It’s the equivalent of:

git branch dev
git checkout dev

In fact, you can even use git checkout to create a branch from any other, not just the one that is currently checked out. For example, to create a new branch called another, from the branch named dev:

git checkout -b another dev
Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest