Undo the last commit but keep your changes
Careful: rewrites history
Short answer
Run git reset --soft HEAD~1. The commit disappears, and everything it changed stays staged, so you can fix it and commit again.
Only for commits you haven't pushed. If others may already have the commit, use Undo a pushed commit instead.
The commands, one by one
- 1
git reset --soft HEAD~1Moves your branch back one commit (
HEAD~1means one before the latest).--softtouches nothing else: the files keep your changes and they stay staged. - 2
git status -sChecks the result: the changes from the undone commit are listed as staged (
AorMin the first column).
For your own repository: Undo more than one commit with HEAD~2, HEAD~3 and so on.