Revert file after commit

What if you accidentally committed file.txt, which you didn’t intend to commit:

git commit -am "Commit all files"

How do you revert the file to its previous state before the commit?

The quick solution is to undo your last commit:

git reset HEAD~

However, you’ll need to recommit everything all over again.

Alternatively, you could checkout the file at a state before it was committed:

git checkout HEAD~ -- file.txt

Then amend your commit:

git commit --amend

Please support this site and join our Discord!