How to Create new Git Branch?

Git branch creation tutorial

This tutorial explains how to create a new branch in Git (locally and remotely). For details, see the section after the general command below.

Suppose you want to create a new branch in Git with the name of “hello-git”. The command for creating a new branch locally:

$ git branch hello-git

A local branch namely “hello-git” should have been created.

A step-by-step guide to creating branches for beginners

If you just require the syntax of how to create a new branch, the above command should work for you. For beginners, what creating a branch involves and what it produces should keep reading the tutorial.

The next section describes the following:

  • Create a repository in Github website
  • Add files in this repo
  • Create a repository in the local machine
  • Running Git Bash on the local machine and pulling the Github repo
  • Create a local branch based on the master branch using Git Bash
  • Creating a remote branch

Step 1: Create a repository on Github

After creating an account on Github website, you may create a repository that stores all your files (including code files). For the demo, I named it “tst-demo”.

Git create repository

If you check the “Initialize this repository with README” option then the new repo should contain a file “README.md”.

For branch testing, let us create a file in this repository so that we can see later what a branch contains.

Git create file

For the demo, I have created ‘file-1’ and just a line of text. Our repository now contains two files as shown below:

Git repo files

Note: If you look at the above graphics (after creating a repo), it shows the master branch is already created. However, for learning how to create branches by command-line, we will create a new branch based on master branch.

Step 2: Working in Git Bash

I have installed Git software on Windows machines with Git Bash. If you have not done so, you may install it by visiting this website and downloading/installing it on your machine.

Now go to the directory where you want to create a local repository. For the demo, I used E drive and created a folder “git-local”.

Open that folder and right click on it and press the “Git Bash Here” option as shown below.

Git bash option

This should open the command line prompt for the Git Bash where you may write commands to work with this version control system.

Step 3: pull Github repo into the local machine

Until now, our local folder contains nothing. First, we will synchronize the Github repository with the local repository.

First of all, run the following command on Git Bash:

$ git init

Now, if you look at the folder, it should show .git folder.

Synchronizing with Github repo

Now run this command to synchronize your remote repository with the local. You have to specify the path that you may get from Github website as shown below:

Git synchronize

The command:

$ git remote add origin “https://github.com/git-test-jaz/hello-git.git”

You have to change the URL as per your repo.

After executing this command in Git Bash, this should show no message.

Git add origin

Use this command to pull files from the remote repository:

$ git pull origin master

This command should pull all files from the “master” branch in the remote repo into your local repo.

See the graphics below for what this command resulted in:

Git pull files

And see what the folder contains now:

Git synched

You can see that the local repo now contains both files that we have in the remote repository (on Github website).

Creating a branch locally

Finally, we reached the topic for what you are here “How to create a new branch”.

Go to Git Bash and write this command to create a new branch based on the master branch:

$ git branch hello-git-branch

This should create a new branch namely “hello-git-branch”. If you want to see the content of this branch then run this command:

$ git checkout hello-git-branch

This command should switch us from the master branch to the newly created “hello-git-branch” branch.

To see what it contains, run this command:

$ ls

To show the difference, the graphic below lists the content of the master branch as well as the newly created branch.

Git branch

You can see, that both branches contain the same content.

Now you may work on the locally created branch independently.

Adding files in local branch example

We have two branches in our local repository now. First, the master branch and the other that we just created i.e. hello-git-branch.

As we checkout and in the hello-git-branch branch. Now, let me add a file in the local repo while working in hello-git-branch branch and see if it affects the master branch or not.

For that, I am adding a new file to the local system folder that we created for our examples. Just to remind you, this folder contains two files i.e. README.md and file-1.

For the demo, I have added another file “file-2.txt” as shown below.

Git branch file

Let me go back to Git Bash and see if both branches are showing this file or not.

I assume you are already in “hello-git-branch” by running this command:

$ git checkout hello-git-branch

After that, run this command to add file-2.txt:

$ git add file-2.txt

This is followed by executing the commit command for committing these changes:

$ git commit –m “add file”

The graphic below shows these commands executed on Git Bash which is followed by the $ ls command.

Git branch commit

You can see that the $ ls command listed three files for the hello-git-branch while only two files for the master branch.

Creating a new branch on the remote server

After you feel comfortable with your code changes and want to commit the changes to the remote repository so all team members can see them, you may use the push command to commit the changes on the remote server.

In our case, we want to create a new branch on the remote server that we just created on the local repo.

For creating the hello-git-branch branch on the remote server (or Github website), run this command on Git Bash:

$ git push origin hello-git-branch

The screenshot below shows how this command executed:

Git branch push remote

And if you look at the Github website account for this repo then it should show two branches as shown below:

Git branch remote

So, our remote repo also contains a newly created branch.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!