git undo

In spite of the title of this post, there is no git undo command, but I bet many among us wish there was one (given that it could read our minds)! I for one keep having to look stuff up every time I screw up and have to undo something. Was it reset? revert? checkout --??

Luckily, I found this very handy article in a book about git the other day. If you did a git oops and need to recover, go read the article! But just for my own (and perhaps your) future reference, here are (some of) the different undo commands collected in one small post (dangerous commands may result in losing code if used incorrectly):

# to unstage a file:
git reset HEAD <file>
# to undo changes and go back to the latest commit (dangerous!):
git checkout -- <file>
# to add a file you forgot to stage to the previous commit:
git add <file> && git commit --amend
# to go back x amount of commits (dangerous!):
git reset HEAD~<x>
# to go back x amount of commits and create a new commit:
git revert HEAD~<x>

Do read the article I linked above before messing with your git tree and checkout this one too for more information about the differences between reset and revert.