To jump back to a previous commit, first find the commit’s hash using [git log](<http://stackoverflow.com/documentation/git/240/browsing-the-history#t=201607251730514686446>)
.
To temporarily jump back to that commit, detach your head with:
git checkout 789abcd
This places you at commit 789abcd
. You can now make new commits on top of this old commit without affecting the branch your head is on. Any changes can be made into a proper branch using either branch
or checkout -b
.
To roll back to a previous commit while keeping the changes:
git reset --soft 789abcd
To roll back the last commit:
git reset --soft HEAD~
To permanently discard any changes made after a specific commit, use:
git reset --hard 789abcd
To permanently discard any changes made after the last commit:
git reset --hard HEAD~
Beware: While you can recover the discarded commits using reflog
and reset
, uncommitted changes cannot be recovered. Use [git stash; git reset](<http://stackoverflow.com/documentation/git/1440/stashing#t=201610192222204220179>)
instead of git reset --hard
to be safe.