Checkout From Specific Git Commit ID Follow the steps to checkout from a specific commit id. Step 1: Clone the repository or fetch all the latest changes and commits. Step 2: Get the commit ID (SHA) that you want to checkout. From your local repository, you can get the commit SHA from the log.
To create a new branch (locally):
With the commit hash (or part of it)
git checkout -b new_branch 6e559cb
or to go back 4 commits from HEAD
git checkout -b new_branch HEAD~4
Once your new branch is created (locally), you might want to replicate this change on a remote of the same name: How can I push my changes to a remote branch
For discarding the last three commits, see Lunaryorn's answer below.
For moving your current branch HEAD to the specified commit without creating a new branch, see Arpiagar's answer below.
All the above commands create a new branch and with the latest commit being the one specified in the command, but just in case you want your current branch HEAD
to move to the specified commit, below is the command:
git checkout <commit_hash>
It detaches and point the HEAD
to specified commit and saves from creating a new branch when the user just wants to view the branch state till that particular commit.
You then might want to go back to the latest commit & fix the detached HEAD:
Fix a Git detached head?
If you want to throw the latest four commits away, use:
git reset --hard HEAD^^^^
Alternatively, you can specify the hash of a commit you want to reset to:
git reset --hard 6e559cb
Just checkout the commit you wants your new branch start from and create a new branch
git checkout -b newbranch 6e559cb95
How can I roll back my previous 4 commits locally in a branch?
Which means, you are not creating new branch and going into detached state. New way of doing that is:
git switch --detach revison
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With