Git: Create a New Branch

In Git, and most other VCS tools, branching is one of the main constructs that really make it useful for software development. These branches are almost like a new copy of your code at the current state, which can then be used to develop new code.

For example, whenever you need to create a new feature, fix a bug, or rewrite any of your code, it's a good idea to create a new branch so that none of your changes affect the "master" version of the code. This is important since it can be very difficult to revert code changes from memory, especially in complex systems.

There are a few ways you can create new branches in Git, with many of them differing in how your branch is created from the main branch, whether it be from your current branch, a different branch, a tag, etc.

The most common way to create a new branch is the following:

$ git checkout -b <branch-name>

This is most commonly used because it will create the branch for you from your current branch and it will switch you to that branch in a single command.

You can also optionally specify a different branch from which the new one will be created:

$ git checkout -b new-branch dev-branch
Switched to branch 'new-branch'

Another common way is by using the branch command directly (which checkout does behind the scenes):

$ git branch <branch-name>

However, as you can see from the following example, this doesn't automatically switch us over to the new branch and keeps us on our current one:

$ git branch
* master
$ git branch new-branch
$ git branch
* master
  new-branch

If you want to work on the branch immediately then you'll need to switch to it manually using the checkout command:

$ git checkout new-branch
Switched to branch 'new-branch'

Creating a Branch from a Commit

As mentioned above, there are a few other ways you can create new branches. One of those ways is by specifying a specific commit via its hash:

$ git branch <branch-name> <hash>
Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

As always with Git, the entire hash doesn't actually need to be specified, just a few characters.

$ git branch
* master
$ git branch commit-branch 735c5b4
$ git branch
  commit-branch
* master

You can also use the git checkout -b <branch-name> <hash> syntax, which will create the branch and check it out, all in one command.

Creating a Branch from a Tag

Much like creating a branch from a commit, you can also create a branch from a tag. This is especially useful since tags are, in my opinion, a better way to reference a certain point in a project's history.

So if you have created tags throughout your project's history, you can create a new branch just like before, but with a tag as the identifier.

$ git branch tag-branch v0.4.12
$ git branch
  tag-branch
* master

And again, the git checkout -b <branch-name> <tag> syntax can also be used.

Last Updated: June 13th, 2019
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms