How can I switch my git repository to a particular commit
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?