Branch from a Previous Commit Using Git

Better Stack Team
Updated on June 24, 2024

To create a new branch from a previous commit in Git, you can follow these steps:

  1. Find the commit hash of the desired previous commit. You can use git log to view the commit history and find the hash of the commit you want to branch from.

     
    git log
    
  2. Copy the commit hash of the desired previous commit.

  3. Create a new branch at the desired commit using the following command:

     
    git checkout -b <new-branch-name> <commit-hash>
    

    Replace <new-branch-name> with the name you want to give to your new branch, and <commit-hash> with the hash of the commit you want to branch from.

For example, if you want to create a new branch named my-branch from a commit with the hash abc123, you would run:

 
git checkout -b my-branch abc123

This command will create a new branch named my-branch starting from the commit with the hash abc123.

Note:

  • The new branch will start from the specified commit and will include all commits leading up to that commit.
  • After creating the new branch, you will be switched to that branch automatically.
  • You can verify that you are on the new branch by running git branch or git status.
  • Make sure to choose an appropriate branch name that reflects the purpose of the new branch.
Got an article suggestion? Let us know
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.