Search⌘ K
AI Features

Creating a Branch

Explore using the git branch command to list all branches and create new ones in your repository. Understand how to identify the current active branch and the importance of switching branches using git checkout to manage your work effectively.

The git branch command

The git branch command is a useful, multi-purpose tool that lets us do a lot of different things. When used as is (without any other options appended), the command will print out all the branches present in the repository and point out which branch we are currently on.

git branch

Let’s use it in the terminal and see what happens.

Terminal 1
Terminal
Loading...

The git branch command will print out the master branch with a prepended asterisk. The asterisk denotes that this also happens to be the branch that is currently active in our repository. In other words, we are currently using the master branch. If there were more branches, their names would also be listed.

Let’s see how we can use the git branch to create a new one.

How to create a new branch

To create a new branch, we will need to modify the git branch command by appending a branch name.

git branch my_new_branch

Now, try to enter this command in the terminal provided below. You can name your new branch anything you like. However, it is preferred that the new branch not have a very long name or spaces in between.

Terminal 1
Terminal
Loading...
You can use the plain 'git branch' command to verify if the new branch was made.
You can use the plain 'git branch' command to verify if the new branch was made.

After you create the new branch, enter the plain git branch command to double-check that it works. You will know if it works because the name will now be printed along with the master branch. One critical point to realize if you want to create a new branch in this manner is that you will still be using the parent branch in your working directory. That should be easy to identify, given the asterisk (*) next to the master branch name.

This is a significant point that you shouldn’t ignore. If you want to now switch to the newly created branch from the master, that is where the git checkout command will help you.