Git and tree branches

In this post, I will show & explain everything you need to know about Git branches. You will learn how to create a new branch, switch to it or checkout to a branch, merge a branch to master, delete a branch, etc.

If Git is not installed on your machine, see how to do it (both in Windows & Mac).

Let’s get started.

Everything you need to know about the Git branches

branches infographic

When you initiate Git, the master branch is automatically created. Also, in your command line, you will automatically point to this master branch just after initiating Git. Thinks of this master branch as the primary, main, live, or production version.

Git master branch infographic

That generally means we do not want to commit or push our work-in-progress code into the master branch (especially when multiple persons work on the same project). Instead, we need to create new branches for each feature or each person. This is generally what Git branches are used for.

How to create a git branch?

Command for creating a new git branch in the terminal
Create a new branch called header-edit

Important: Before you create a new Git branch, you should have a clean working tree. That means you have to commit changes before you create the new branch.

To create a git branch, type the following command in your terminal or command line:

git branch name_of_the_branch

If you want to create a new git branch named “header-edit”, you have to type the following command:

git branch header-edit

How to checkout or switch to the new branch?

To switch to another branch, type the following command:

git checkout name_of_the_branch

Let’s say, you want to switch to the new branch called “header-edit”, so your command will be as follows:

git checkout header-edit

In Git, switching to another branch refers to checkout.

How to create & switch to a new branch in one single command?
Single command for creating a new Git branch and point to it
Here * (star symbol) means the current branch

Let’s say you want to create a new Git branch and at the same time you want to switch to the new branch in a single command. Assume that the new branch name will be “sidebar.” The command will be as follows:

git checkout -b sidebar

The above command will create a new Git branch called “sidebar” and at the same time, your terminal will point to this newly created “sidebar” branch.

Learn more about switching between git branches.

How to list all branches in Git?

list of git branches
The command for displaying the list of Git branches

To list all branches in Git, type the following command:

git branch

The above command will list down or show all the available/current branches you have.

In the above screenshot, you see that there is a “*” (star) symbol before the “sidebar” branch. It means that your terminal is currently pointing to the sidebar branch.

If you need access to another branch, then you have to “checkout” to that branch that I discussed in the earlier section.

What is the difference between the Git branch & remote branch?

A Git branch typically refers to the branch that you created locally (in your machine/computer). On the other hand, a remote branch refers to the branch that exists in the Git hosting server like GitHub, Bitbucket, etc. Learn more about using Git and GitHub.

You can create a Git branch locally and also on the remote server.

How to delete a branch?

Deleting a Git branch
Deleting a Git branch called “header-edit”

After merging a feature branch with the master, there is no reason to keep it (feature branch). So mostly you need to delete it after.

To delete a Git branch, type the following command:

git branch -d name_of_the_branch

For example, if you want to delete a branch called “header-edit”, the command will be as follows:

git branch -d header-edit

How to merge a Git branch to the master?

Git merging concept infographic

To merge a feature branch to the master, type the following command:

git merge name_of_the_branch

Let’s say, you want to merge the “sidebar” branch to the master, the command will be the following:

git merge sidebar

See more detail about merging a feature branch to the master.

How to push to a specific branch?

To push to a specific branch, type the following command:

git push origin name_of_the_branch

For example, if you want to push your progress to the “footer” branch, type the following command:

git push origin footer

Video instruction

Conclusion

Now you know all sorts of information about the Git branch and mostly used commands. If you did not find any related information, or if you have any questions, let me know.