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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github