To create a new branch in Git, use this command: git branch <branch-name>. This allows developers to work on distinct features, bug fixes, or experimental changes without directly impacting the main line of development.

Git branches provide a mechanism for divergence, enabling parallel development efforts and facilitating collaborative work within a codebase. This powerful feature promotes a structured and organized approach to version control, allowing for efficient management of different development threads and easy integration of changes when ready.

This is particularly useful for collaborative software development, where multiple developers are contributing to a codebase.

Git Create a New Branch

Let’s take a look at the basic command to create a new branch.

Basic Commands for Creating a New Branch

Creating a new branch in Git is a straightforward process. Here is the basic command to create a new branch:

git branch new-feature
Bash

This command only creates a new branch, but does not actively switch to it. To work on the newly created branch, you need to execute another command to set it as the active branch, which is git checkout.

Let’s explore the section below to learn how to create a new branch based on a specific point.

Creating a New Branch Over a Specific Commit

To create a new branch over a specific commit in git, you can use the git branch command with the commit hash. This will create a new branch pointing to the specified commit, effectively creating a new branch at that point in the project’s history.

Here is the command:

git branch <new-branch> <commit-hash>
Bash

By running this command git reflog, you can view the commit history and identify the commit you want to use as the base for your new branch.

git reflog

Once you have identified the commit, you can create a new branch based on it using the git branch command, followed by the SHA of the commit.

For example, let’s say we want to create a new branch based on the commit with the SHA 70c5b73. To do that, just run the following command.

git branch new-branch-name 70c5b73
Bash

This command creates a new branch called new-branch-name based on the commit with the SHA 70c5b73.

Create a new branch at refs

You can then switch to the new branch using the git checkout command and start making changes as usual.

Overall, this command is a useful way to view the history of refs and identify the commit you need to create a new branch based on.

Nevertheless, the main question is still unanswered: how to push all these changes to the remote server? You can get the answer by reading this article, which explains how to push the newly created branches to the remote server.

In the following section, you will learn how to create a new branch based on a tag in the workflow.

Create a New Branch From a Tag in Git

Additionally, it’s worth noting that you can create a branch from a tag in Git. This is useful when you want to work on a specific version of your codebase. To create a new branch from a tag, use the git checkout command followed by the -b flag, the name of the new branch, and the name of the tag. Here’s an example:

git checkout -b new-branch-name tag-name
Bash

This command will create a new branch called ‘new-branch-name’ based on the commit referenced by the tag ‘tag-name’ and switch to it. From here, you can make changes and commit them as usual.

If you don’t have a tag name that matches the commit you want to create a branch from, you will receive an error message. This indicates that you need to either create a tag first or ensure you have the correct tag name before creating a branch from it.

To list all available tag names in a Git repository, you can use the following command:

git tag
Bash

This will display a list of all tag names in alphabetical order. If you want to see the tags sorted by their commit dates, you can use the following command:

git tag --sort=-committerdate
Bash

Let’s summarize it.

Wrapping Up

Git branching is a powerful feature that facilitates collaborative software development by allowing developers to work on different aspects of a project simultaneously without interfering with each other’s work. Branching enables the divergence from the main line of development, providing a mechanism for making isolated changes.

The basic command to create a new branch in Git is straightforward:

git branch new-feature
Bash

However, it’s important to note that this command only creates a new branch without switching to it. To actively work on the newly created branch, developers need to execute another command, such as git checkout.

Creating a new branch based on a specific commit is a common scenario. The following command accomplishes this:

git branch <new-branch> <commit-hash>
Bash

By using the git reflog command, developers can view the history of refs, including branch checkouts, commits, merges, and more. This history aids in identifying the desired commit to serve as the base for the new branch.

To create a new branch based on a tag, developers can use the following command:

git checkout -b new-branch-name tag-name
Bash

This command creates a new branch named ‘new-branch-name’ based on the commit referenced by the tag ‘tag-name’ and switches to it. It’s essential to ensure that the correct tag name exists before creating a branch from it.