How to revert previous commit in git?

Technology CommunityCategory: GitHow to revert previous commit in git?
VietMX Staff asked 3 years ago

Say you have this, where C is your HEAD and (F) is the state of your files.

   (F)
A-B-C
    ↑
  master
  1. To nuke changes in the commit:
git reset --hard HEAD~1

Now B is the HEAD. Because you used –hard, your files are reset to their state at commit B. 2. To undo the commit but keep your changes:

git reset HEAD~1

Now we tell Git to move the HEAD pointer back one commit (B) and leave the files as they are and git status shows the changes you had checked into C. 3. To undo your commit but leave your files and your index

git reset --soft HEAD~1

When you do git status, you’ll see that the same files are in the index as before.