304 North Cardinal St.
Dorchester Center, MA 02124
A quick tutorial on how to use Git to create a new branch from the master or current branch. It works for Github, Gitlab, and any other VCS platforms also.
So I am assuming that since you are asking the question of creating a new git branch, you might also be aware of what GIT and VCS are.
In this tutorial, I will be listing down some of the easiest ways of git to create a new branch from the current branch. However, all of them will involve the steps to do it from the command line.
Why the command line? Because once you know how to do it from the command line, you can do it for any VCS platform be it Github, Gitlab, or Atlassian git. This way the steps become universal for usage.
If I was to make a command for creating new branches, I would have made a command like “git create branch <branch-name>”.
Do you see how readable and intuitive it is? Well, but to our surprise, this is not the command that exists. Instead, the command to do it is a bit more complex.
So if “n” developers are working on a project and they want to make changes to the same codebase without affecting each other’s code, what is the way to do so?
Most probably, take a copy of the latest code and do the changes on top of that. Once done update the latest copy with your changes. This is exactly what a branch in Git does.
It gives you the flexibility of working on the same codebase with the “N” number of other developers.
To understand Git branch better way and in-depth, have a look at this guide from official Git docs on branching.
So, let’s get started on how can create a branch in git.
[su_label type=”success”]Suggested read[/su_label] Git installation on windows 10
The simplest answer to the problem is this command. You can create a new branch using the command.
git checkout -b <branch-name>
Alternatively, you can use two commands to create a branch and then checkout so that you can start working on it.
git branch <branch-name> git checkout <branch-name>
However let’s first understand what does this checkout command means, and then how can we create a new branch from current or master branches.
checkoutcommand will find the latest commit on the branch from which you want to create a new branch.
With this latest commit, it will update all of the files on your computer’s hard drive to match the changes of all the files that particular latest commit has.
When a new branch is created it will move the pointer to the top of the HEAD which will be your latest commit. This is to ensure that whenever you make a new commit on the new branch it is made on top of the changes that you just created branch from.
checkoutcommand is normally used to checkout already created branches. But as soon as you pass
-bflag to it, Git understands it as a command to create a new branch with the given branch name.
In case the branch name you give already exists, it will not overwrite changes but fails the command and will not create a new branch with the same name.
[su_label type=”success”]Suggested read[/su_label] Git checkout remote branch to local
To create a GIT branch from the master, you can use these commands sequentially.
git checkout master git pull git checkout -b <New_branch_name>
How this works:
To create a GIT branch from the current branch, you can use three commands sequentially and expect git to create a new branch for you.
git checkout master git pull git checkout <already_exisiting_branch> git checkout -b <New_branch_name>
How this works :
There are few other ways you can create a branch. One is from a single commit and the other one is from any release tag.
To create a branch from a commit, we can use simply pass the commit hash to the checkout command. This will ignore all the changes made above and after that commit.
So your new branch will only have changes until the commit you specify. It will ignore all the changes post that.
Here’s what the command looks like.
git checkout master git pull git checkout -b <New_branch_name> <commit_hash_id>
Similar to creating a branch from commit, you can also create a commit from one of the release tags.
Here is how the command looks like.
git checkout master git pull git checkout -b <New_branch_name> <tag_version>
git checkout -b new_branch v1.0.0
[su_label type=”success”]Suggested read[/su_label] How to Git delete local branch
When you will create a new branch from either of the commands listed above, it will create a branch in just your local computer.
Assuming you would have made some changes to the branch and made some additional commits, now you would want to push the changes to the remote repository right.
Here is a command that will help you to do that.
git push origin <new_branch_name_you_created>
This is assuming you are already on your local branch.