Git undo cheatsheet

Posted by Markus Benning on August 19, 2017

This is a cheat sheet for undoing changes in git.

Local Changes

vim example.txt

Undo with:

git checkout -- example.txt # fetch file from repository
git stash [save <description> # save for later
git reset --hard # reset whole repository

Staged Changes

git add example.txt

Undo with:

git reset HEAD example.txt

Committed Changes

git commit

Undo with:

git revert HEAD  # with revert commit
git commit --amend # redo last commit with next commit
git reset HEAD^ --hard # remove last commit

Remote Changes

git push

Undo with:

git revert HEAD ; git push # revert in new commit
git reset HEAD^ --hard && git push origin -f # remove commit and force push