How to create new Git branch?
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 new branch locally:
$ git branch hello-git
A local branch namely “hello-git” should have been created.
A step by step guide of creating branches for beginners
If you just required the syntax of how to create a new branch, the above command should work for you. For beginners, what actually 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 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”.
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.
For the demo, I have created ‘file-1’ and just a line of text. Our repository now contains two files as shown below:
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 download/install 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 “Git Bash Here” option as shown below.
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 for synchronizing your remote repository with local. You have to specify the path that you may get from Github website as shown below:
$ 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.
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 what this command resulted:
And see what the folder contains now:
You can see, 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 the Git Bash and write this command for creating 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 newly created “hello-git-branch” branch.
For seeing what it contains, run this command:
For showing the difference, the graphic below lists the content of master branch as well as the newly created branch.
You can see, 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, 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.
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 $ ls command.
You can see, 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 it, you may use the push command for committing 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:
And if you look at the Github website account for this repo then it should show two branches as shown below:
So, our remote repo also contains newly created branch.