Branch from a Previous Commit Using Git
To create a new branch from a previous commit in Git, you can follow these steps:
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
Copy the commit hash of the desired previous commit.
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
orgit status
. - Make sure to choose an appropriate branch name that reflects the purpose of the new branch.
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 usBuild 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.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github