Git

How to Create Branch From a Commit in Git

On Git repositories, multiple people work together as a team. However, to add a new feature and make changes to an existing repository, you can create new branches from another branch and the Git commit history. Git branches are also used to isolate the specific Git commits from the main log history. For instance, if the master branch contains the main Git log history, you can create a separate Git branch to add new features.

This blog will discuss the procedure of creating a branch from a commit in Git.

How to Create Branch From a Commit in Git?

On Git, utilizing the specific “commit SHA” from Git history can assist in creating a new branch. Commit SHA is also known as “commit reference” generated by Git when users make changes in the local repository and commit them to the remote repository.

Follow the below steps for creating a branch from a commit in Git.

Step 1: Open Git Bash
Open up “Git Bash” with the help of the “Startup” menu:

Step 2: Navigate to Git Directory
Move to the Git local directory using the “cd” command:

$ cd "C:\Users\nazma\My_branches"

Step 3: Check Branch List
Check the list of branches that exist in the specified Git local directory utilizing the “git branch” command with “-a” flag:

$ git branch -a

As you can see, our “My_branches” Git repository contains two branches, and currently we are working in the “master” branch:

Step 4: Check Branch Log
After that, run the “git log” command to check the commit history:

$ git log

Note: Above command will display all commit history. From the given output, copy the “commit hash” of the specific commit from which you want to create a Git branch:

Step 5: Create Branch Using Commit Hash
Now, create the new branch by executing the “git checkout” command and specify the “commit hash”. Here, the “-b” flag means “branch” used to create the branch. However, “alpha” is the branch name, and “a07b638” is the commit hash or reference of the specific commit:

\
$ git checkout -b alpha a07b638

Below image indicates that we have successfully created a new branch from the commit:

Step 6: Verify Created Branch
Lastly, verify the created branch:

$ git log --oneline --graph

As you can see, we have successfully created “alpha” branch using the commit reference:

You have learned the easiest method for creating a branch from a commit in Git.

Conclusion

To create a branch from a commit in Git, first, navigate to the Git directory or repository and run the “$ git log” command to check the commit history of the currently used Git repository, select one of them and copy the Commit reference. After that, create the branch by executing the “$ git checkout -b” command and specify the copied commit hash or references. In this blog, we have demonstrated the process of creating a branch from a commit in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.