Open In App

How to Create a New Branch in Git and Push the Code?

Improve
Improve
Like Article
Like
Save
Share
Report

Branching in Git is a helpful feature for software developers working on a big team project. It allows the team members to work on different aspects of the software by creating a branch from the main branch. The main branch is not affected by the changes in the created branch until it is merged into the main branch. Once, work is complete for the branch, it can be merged into the main branch to incorporate the changes. It can be used for better management of files related to the software. It also helps us try or experiment with adding some new features to the code; if it works well, it can be merged with the main code in the main branch. In this article, we will learn how to create a new branch in Git and push the code to it.

What Is The Command To Create a New Git Branch?

To create a new git branch you can simply use the following command

git branch <Branch name>

It will simply create the new git branch with the customized name. From this branch, you can push and pull the code from the remote repository and perform the changes according to your requirements.

How to Create a New Git Branch from the Existing Branch or Current Changes?

There are many ways where you can create a new branch following is one of the easier ones for creating a new branch from the existing branch.

The command which is mentioned in the following will create the new git branch and it will switch to that branch. The new branch will be created from the current commit. It easiest way because it will create a new branch and it will directly checkout to that branch.

git checkout -b <new_branch_name>

Replace the <new_branch_name> with the name of the branch you want.

Create A New Git Branch and Checkout

In this first you need to create an new git branch by using the following command where you can work with your code and you need to another command to check out to the new branch.

1. Create a New Git Branch

Use the following command to create an new git branch.

git branch <branch_name>

2. Checkout To the Newly Created Branch

Use the following command to checkout to that branch.

git checkout <branch_name>

How to Push New Git Branch To Remote?

Pushing the git branch to the remote repository will helps you in many different ways as.

  • Other developers can pull the code and work on it.
  • The code which you have been written will been maintained securely in the remote repository.

Flowwing is the command which is used to push the git branch to the remote location.

git push <remote_name> <branch_name>

IN the place of <remote-name> replace with the remote Url and in the place of <branch_name> give the branch name.

How to Switch to New Git Branch?

To switch new git branch you can use the following command.

git checkout <branch_name>

In the place of <branch_name> give the branch name which you want to switch.

Git Checkout

Git checkout command is used to switch between different branches or to the required branches which are created by using the “git branch” command. Checking out between different branches can be useful to restore the files of different branches.

To switch to the different branches you can use the following command.

git checkout <branch-name>

And also you can restore the files which required specifically to the previous version you can use the following command.

git checkout <commit-hash> <file-name>

Checking Out Branches

Git check pout command will helps you to navigate between the different branches which are created with the following command.

git branch

For example if you you are currently in the “dev branch” and you want to checkout to the “prod branch” then you can use the following command.

git checkout prod

If there is no branch available with the name of prod then git will create automatically when you are checking out to the new branch.

Usage: Existing branches

If you are working on some code of an existing repo and you want to know the current branch name and you want to know the how many branches are available in that repo and name of that branches then you use the following commands.

Following command will list all the branches available in the git repository.

git branch

output:

*DEV

TEST

PROD

The asterisk (*) represents the current branch that you are working.

Steps to Create How To New Branches And Push To GitHub

Step 1: For creating a branch, you must ensure that you have a GitHub repository

So, let us assume that you have a repository for DSA problems solved by you. You can create different branches in this for different topics. Once a topic is completed, you can merge the branch into the main branch.

GitHub Repository

 

Now, we have a repository having a folder containing codes for Linked List problems. We are currently in the master branch or main branch. We will now create a branch to add codes for Tree problems in it.

Step 2: To create a new branch, open the terminal and ensure that you are in your working directory

Now, you can type “git branch <branch-name>” in the terminal(without quotes) and hit enter as shown.

Master branch

 

Now, we can check the branches we have, by typing “git branch” in the terminal.

Different branches available

 

It is visible from the above snapshot that currently, we have 2 branches namely “master” and “Tree-problems” and we are in our master branch for now.

Step 3: Now, we need to switch to the newly created branch

To switch between the branches in Git, we can simply type “git checkout <branch-name>” in the terminal, where <branch-name> is the branch to which you want to switch into.

Checkout to tree branch

We can see that we are in our “Tree-problems” branch now.

Step 4: Make changes in the newly created branch

Now, we can make changes to our branch as we normally do in Git using add, commit, and push commands.

git commit

Step 5: Merge the created branch with the master branch

The created branch can be merged with the main branch by typing “git merge <branch-name>” in the terminal, where the <branch-name> is the name of the branch that needs to be merged in the master branch.

Merge the branch

In this way, we can create as many branches as we want for our project and once we are done working on them separately, we can merge those branches into the main/master branch.

Switching Branches

Switching form one branch to the other branch is an straightforward operation it can be done by using single command and single step as mentioned following command.

git checkout <branchname>

Git Checkout a Remote Branch

When you are working with a team for any project it is common to fetch the repositories which are available in the remote location to you local machine when you are fetching the branch from remote repositories then you are going to fetch lots of new files which are developed by the other developers In order to checkout a remote branch you have to first fetch the contents of the branch.You can use the following command to fetch the new repositories each repositories will have there own new set of branches.

git fetch --all

The new versions of the git will allows you to check out checkout the remote branch like a local branch. By using following command.

git checkout <remotebranch>

In the older versions of the git it will create an new branch and you need to check out that branch for that you can use the following command.

git checkout <remote-name>/<branch-name>

Detached HEADS

When the HEAD pointer in Git is not pointing to a branch, it is known as a detached HEAD state. Rather, it is referring to a particular commit. This can be helpful for a number of things, such cherry-picking commits, examining a particular commit, or resolving a bug that was introduced in an earlier commit. You can use the following command to get detached head.

git checkout <commit-hash>

Replace the <commit-hash> with the hash of the commit you want to checkout.

FAQs On Creating New Branch In Git

1. How To Create a New Branch In Git From Existing Branch?

Checkout to the existing branch by the following command.

git checkout <existing-branch-name>

Create a new branch

git checkout -b <new-branch-name>

2. How To Create A New Branch In Git Without History?

The command to create an new branch without history is as following.

git checkout --orphan <new-branch-name>

3. How To Create A New Branch In Git From A Specific Commit?

You can use the following command to create a new branch In git from a specific commit.

git checkout -b <new-branch-name> <commit-hash>

4. How To Create a New Branch In Github?

Follow the steps mentioned below to create an branch in GitHub.

  1. Navigate to the GitHub repository in which the new branch is to be created.
  2. Click the Code tab.
  3. Under the Branches dropdown menu, click New branch.
  4. In the Branch name field, enter a name for your new branch.
  5. Click Create branch.



    Last Updated : 15 Jan, 2024
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
    Similar Reads