Git Undoing Return to a previous commit

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

To jump back to a previous commit, first find the commit's hash using git log.

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 instead of git reset --hard to be safe.



Got any Git Question?