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:
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:
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:
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:
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.