The Wayback Machine - https://web.archive.org/web/20240829110019/https://www.geeksforgeeks.org/how-to-create-a-new-branch-in-git-and-push-the-code/
Open In App

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

Last Updated : 15 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



    Previous Article
    Next Article

    Similar Reads

    Difference Between Git Push Origin and Git Push Origin Master
    Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
    3 min read
    Git - Create a Branch From Another Branch
    Branching in Git allows developers to work on isolated features or fixes without affecting the main codebase. However, what if you need to create a new branch based on the changes in another branch? That's where the concept of branching from another branch comes into play. In this guide, we'll see the process of creating a branch from another branc
    3 min read
    How to Create a Branch In Git from Another Branch?
    Branching is a fundamental aspect of version control systems like Git, which helps developers work on multiple features or bug fixes simultaneously without interfering with the main codebase. Creating a branch from another branch is a common practice, especially when you want to build upon existing work or isolate changes for a specific feature. In
    3 min read
    How to Make an Existing Git Branch Track a Remote Branch?
    Managing branches in Git can sometimes be confusing, especially when you need to make an existing local branch track a remote branch. This process ensures that your local branch can easily fetch updates from and push changes to the corresponding remote branch. In this article, we will walk through the steps to set up tracking for an existing Git br
    3 min read
    How to Compare a Local Git Branch with its Remote Branch ?
    When working with Git, it's often necessary to compare your local branch with its remote counterpart to understand the differences in terms of commits and changes. This helps in keeping your local work in sync with the remote repository and managing potential conflicts. This article will guide you through various methods to compare a local Git bran
    3 min read
    How to Make the Current Git Branch a Master Branch?
    Managing branches effectively is a key part of using Git for version control. Occasionally, you might find yourself in a situation where the current branch you're working on should be the new master branch. This could be due to the current branch evolving to become the main line of development, or simply a restructuring of your project's branch str
    3 min read
    How to Replace Master Branch with Another Branch in GIT?
    In Git, the "master" branch traditionally serves as the primary development branch in many repositories. However, there are situations where you might want to replace the contents of the "master" branch with those of another branch. This could be due to a variety of reasons, such as renaming the default branch to something more inclusive or reflect
    2 min read
    Git Merge Hotfix Branch Into Feature Branch
    When working on a software project, you may encounter situations where a hotfix needs to be applied to a feature branch. A hotfix typically addresses urgent issues in the codebase that need immediate attention and correction. Integrating these changes into a feature branch ensures that the fixes are incorporated into ongoing development, maintainin
    4 min read
    How To Replace Local Branch With Remote Branch in Git?
    Managing branches in Git can sometimes require drastic measures, such as replacing a local branch entirely with its remote counterpart. This can be useful in scenarios where the local branch has diverged too much from the remote branch, leading to complexities that are easier to resolve by completely overriding the local branch. This article will g
    4 min read
    How to Push Git Branch to Remote?
    Git is the most popular version control system which records the changes made to our project over time in a special database called a repository. We can look at our project and see who has made what changes when and why and if we screw something up we can easily revert our project back to an earlier state. In this article, we will see the essential
    6 min read
    How to Push a Local Branch to a Remote Repository in Git?
    Git is a popular version control system that allows you to track changes in the codebase and collaborate with others. One of the common tasks in Git is pushing a local branch to a remote repository. This article will guide you through the steps to achieve this. Pushing Local BranchPushing a local branch to a remote repository involves creating a br
    2 min read
    How To Resolve "Git push rejected after feature branch rebase"?
    Rebasing feature branches in Git can lead to a cleaner commit history and better integration with the main branch. However, it's not uncommon to encounter rejections when pushing rebased branches due to conflicts with remote changes. In this article, we'll explore the common causes of "Git push rejected after feature branch rebase" errors and provi
    3 min read
    Default Behaviour Of "git push" Without A Branch Specified
    When working with Git, understanding how Git push operates without specifying a branch can prevent many potential pitfalls in version control. This article will guide you through the default behaviour of Git Push Without A Branch Specified. What is git push?git push is a fundamental Git command used to upload local repository content to a remote re
    3 min read
    How to Create a New Branch in Git?
    Git is a powerful and widely used version control system that helps developers manage code changes across projects efficiently. One of the fundamental features of Git is branching, which allows developers to diverge from the main line of development and work on different tasks or features independently. This guide will walk you through the process
    4 min read
    Difference Between "git commit" and "git push"?
    Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break down their differences. What is git commit?git commit
    2 min read
    How to Move Existing, Uncommitted Work to a New Branch in Git?
    Git is a widely used distributed version control and source code management system. A common scenario developers face is needing to move their uncommitted work to a new branch. This can be important for maintaining organized workflows, especially when realizing that ongoing changes belong to a separate feature or bug fix. In this article, we will s
    2 min read
    How to Move the Most Recent Commit(s) to a New Branch With Git?
    Git offers powerful features for managing your codebase efficiently. Sometimes, you may find yourself needing to move the most recent commit(s) to a new branch in Git. This could be due to various reasons, such as realizing that changes were made on the wrong branch or wanting to isolate experimental work. Fortunately, Git provides simple ways to a
    4 min read
    How To Publish A New Branch In Git?
    Git is a distributed version control system, offering powerful tools for collaboration, version tracking, and branching. One of its key features is branching, which allows developers to work on different features or fixes simultaneously without interfering with the main codebase. This article will guide you through the process of publishing a new b
    4 min read
    Difference Between Git remote prune, Git prune and Git fetch --prune
    Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
    4 min read
    How To Find Out Which Remote Branch A Local Branch Is Tracking?
    Git is a powerful version control system that allows developers to manage and track changes in their code repositories. One of the key features of Git is its ability to track relationships between local and remote branches. Knowing which remote branch a local branch is tracking can help you manage your workflow more efficiently, especially when col
    3 min read
    How to Create a Remote Git Branch?
    Creating a remote Git branch allows multiple developers to work on different features or fixes simultaneously. It ensures a clean and organized workflow, enabling collaboration without affecting the main codebase. This article covers the steps to create a remote Git branch and push it to a remote repository like GitHub. Table of Content Approach 1:
    2 min read
    How to Create a Git Branch From Unstaged/Uncommitted Changes on Master?
    When working with Git, you might find yourself in a situation where you've made changes to your files but haven't committed them yet. You realize that these changes should be in a new branch rather than the current one, typically master or main. This guide will show you how to create a new branch and move your unstaged or uncommitted changes to it
    3 min read
    How to Create Git Branch With Current Changes?
    Creating a new branch in Git while preserving your current changes is a common task that allows you to work on new features, bug fixes, or other tasks without affecting the main codebase. This article will guide you through one approach to achieve this, providing a detailed description, syntax, and examples for each method. Stashing ChangesStashing
    1 min read
    How To Create Branch In Git?
    Creating branches in Git is a fundamental skill for any developer. Branches allow you to work on different features, bug fixes, or experiments in isolation from the main codebase. This way, you can keep your main branch stable while making changes on separate branches. In this article, we’ll guide you through the process of creating and managing br
    2 min read
    How to Create Branch From a Previous Commit Using Git?
    Creating a branch from a previous commit in Git is a common task that allows developers to revisit and build on a specific point in their project's history. This can be useful for bug fixes, new feature development, or exploring alternate project paths without affecting the current state of the project. In this article, we will explore methods to c
    2 min read
    Git Create Branch
    Branching is one of the core features that make Git a powerful version control system. Branches allow you to work on multiple features, bug fixes, or experiments simultaneously without interfering with the main codebase. In this article, we’ll learn the process of creating branches in Git. What Are Git Branches?In Git, a branch is basically a point
    3 min read
    How to Create a New Branch on Github using Pycharm?
    Git is an open-source version control system. It means that whenever a developer develops some project (like an app or website) or something, he/she can constantly update, Git is a version control system that lets you manage and keep track of your source code history. Let’s say you have a project, and you have uploaded that on GitHub and now want t
    2 min read
    How to Create a New Branch on GitHub using Android Studio?
    Creating a new branch on GitHub using Android Studio can help your development workflow by allowing you to work on new features or fixes without affecting the main codebase. In this article, we will walk you through the steps to create a new branch directly from Android Studio and push it to your GitHub repository. Note: To Upload Project on GitHub
    2 min read
    Difference Between Git HEAD and Main Branch
    In Git, understanding the roles of the HEAD and the primary branch (commonly master or main) is important for efficient version control and collaboration. These concepts, while related, serve different purposes in the management of repositories. In this article, we will explore what HEAD is and the primary branch are, their syntax, uses, and featur
    3 min read
    How Delete a Git Branch Locally and Remotely?
    In Git, branches are an important part of the workflow, allowing developers to work on different features, bug fixes etc. without affecting the main codebase. However, once a branch has served its purpose, it is a good practice to delete it to keep the repository clean and organized. This article will guide you through the steps to delete a Git bra
    4 min read
    Article Tags :