How to Create a New Branch in Git From Another Branch

Avatar

By squashlabs, Last Updated: October 27, 2023

How to Create a New Branch in Git From Another Branch

Creating a new branch in Git from another branch is a common operation when working on a software project. It allows you to work on new features or bug fixes without affecting the main branch. In this guide, we will walk through the step-by-step process of creating a new branch in Git from another branch.

Step 1: Checkout the Branch

Before creating a new branch, you need to make sure you are on the branch from which you want to create a new branch. You can use the git branch command to check the current branch and switch to the desired branch using the git checkout command.

Here is an example:

$ git branch
* main
  feature-branch

$ git checkout main
Switched to branch 'main'

Related Article: How to Use Git Stash Apply Version

Step 2: Create the New Branch

Once you are on the desired branch, you can create a new branch using the git branch command followed by the name of the new branch. Make sure to provide a meaningful name that describes the purpose of the new branch.

Here is an example:

$ git branch new-branch

This will create a new branch named “new-branch” based on the current branch.

Step 3: Switch to the New Branch

After creating the new branch, you need to switch to it using the git checkout command. This will allow you to start working on the new branch.

Here is an example:

$ git checkout new-branch
Switched to branch 'new-branch'

Now you are on the newly created branch and any changes you make will only affect this branch.

Step 4: Push the New Branch to Remote

If you want to share the new branch with others or backup your work, you can push the new branch to a remote repository. This will make the new branch available to others who have access to the remote repository.

Here is an example:

$ git push origin new-branch

This will push the new branch named “new-branch” to the remote repository named “origin”.

Related Article: How to Stash Untracked Files in Git

Alternative: Create and Switch to a New Branch in One Step

Alternatively, you can create and switch to a new branch in a single step using the git checkout command with the -b option followed by the name of the new branch.

Here is an example:

$ git checkout -b new-branch
Switched to a new branch 'new-branch'

This command will create a new branch named “new-branch” based on the current branch and switch to it in one step.

Best Practices

When creating a new branch in Git from another branch, it is recommended to follow these best practices:

1. Choose a descriptive and meaningful name for the new branch that reflects its purpose.
2. Keep the branch names consistent and easy to understand across the team.
3. Regularly update your local branch with the latest changes from the parent branch using the git pull command.
4. Commit your changes frequently and write clear and concise commit messages.
5. Push your branch to a remote repository to share your work and collaborate with others.

More Articles from the Git Tutorial: From Basics to Advanced Concepts series:

How to Remove Files From a Git Staging Area

Removing files from Git's staging area is a simple process that can help you manage your repository more efficiently. This article provides practical examples and... read more

How to Move Recent Commits to a New Branch with Git

Guide to relocating recent commits to a new branch using Git commands. Learn two methods: using git branch and git cherry-pick commands, or using the git rebase command.... read more

How to Remove a File From a Git Repository

Deleting files from a Git repository is a common task for software developers. This article provides two methods for removing files: using the git rm command and using... read more

How to Force Overwrite During Git Merge

This article provides a step-by-step guide on how to force overwrite during a Git merge operation. It covers two methods: using the --strategy-option flag and using the... read more

How To Name And Retrieve A Git Stash By Name

Naming and retrieving a Git stash by name is a fundamental skill for effective version control. This article provides a simple guide on how to accomplish this task. It... read more

How to Rename Both Local and Remote Git Branch Names

Renaming Git branch names locally and remotely can be done with ease using a few simple methods. This guide provides step-by-step instructions on how to achieve this... read more