Signup/Sign In

How to Create Git Branches

A branch is a pointer to one of the commits in our repository. Branches are an important part of any Git workflow. They provide an independent workspace for developers where they can experiment with things and build new features without worrying about corrupting the rest of the project. These branches can then be merged with each other or with the master branch.

Let's learn how to create branches in Git.

Branches in Git

Creating Branches

Branches in Git can be created using two different commands - Git Branch and Git Checkout. The difference between them is that Git Branch will simply create a new branch whereas Git Checkout will create a new branch and also move our HEAD to the branch(we will be checked out on the newly created branch).

Let's learn how to use them for different scenarios.

Difference between Git Branch and Git Checkout

A New Branch from our Current HEAD

The HEAD is a pointer to our currently checked-out branch. It points to the most recent commit of our current branch.

To create a new branch based on the HEAD we can use the following Git Branch command. We can use the Git Checkout command or the Git Switch command in the future to switch to this new branch.

$ git branch <new-branch-name>

We can use the Git Checkout command to create the new branch and also switch to that branch. Use the -b flag with the command to accomplish this.

$ git checkout -b <new-branch-name>

A New Branch from a Commit

A branch can also be created based on some other previous commit point. Git will need the hash of that commit to create a new branch based on it.

$ git branch <new-branch-name> <commit-hash>

We can also use the Git Checkout command to create and move to that branch with a single command.

$ git checkout -b <new-branch-name> <commit-hash>

A New Branch from a Tag

Tags are used to mark specific points in the history of our project. In most cases, tags are used to mark release versions of our project like v1.1, v1.2, etc. We will need the name of the tag to create a new branch based on it. To view the tag names we can use the Git Tag command.

$ git tag

Next, we can use either Git Branch or Git Checkout to create a new branch based on that tag. Git Checkout will make this new branch our current working branch.

$ git branch <new-branch-name> <tag-name>
$ git checkout -b <new-branch-name> <tag-name>

A New Branch from an Existing Branch

We can also create a new branch from the tip of an existing branch. We need the existing branch's name to do this. Use the Git branch command with the -a flag to view all the branches present in the repository.

$ git branch -a

Run the Git Branch or the Git Checkout command with the name of the existing branch to create a new branch based on it.

$ git branch <new-branch-name> <existing-branch>
$ git checkout -b <new-branch-name> <existing-branch>

A New Branch from a Remote Branch

A Remote Branch is a branch that exists in the remote repository. Whenever we fetch from the remote repository, a Remote Tracking Branch is created that tracks changes of the remote branch. We will need this Remote Tracking Branch to create a new local branch based on the remote branch.

The remote branch is called the Upstream Branch of our local branch and creates a tracking connection between the two. This also makes it very easy to run Git Push and Git Pull commands for these two branches easier.

$ git branch --track <new-branch-name> <remote-tracking-branch>
$ git checkout --track -b <new-branch-name> <remote-tracking-branch>

A New Branch in the Remote Repository

We can also create a new branch in our remote repository by pushing an existing local branch. This will make the remote branch an Upstream Branch for our local branch. We need to use the Git Push command with the -u flag to do this.

$ git push -u <remote-name> <branch-name>

Summary

Branches are a very useful tool in Git as they give developers the freedom to work on new features without worrying about affecting the rest of the project. Branches can be created using the Git Branch command or the Git Checkout command.

Git Checkout will also take us to the newly created branch with a single command but with Git Branch we also need to use Git Switch or Git Checkout to switch to the new branch. A new branch can be based on any commit, any tag, or any other branch. We learned how to create a new branch in a lot of different scenarios.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight