Undo your last commit but keep the changes
Committed too early or on the wrong branch? Take the commit back without losing a single line.
- Run
git reset --soft HEAD~1. - The commit is gone from
git log, but your changes are still there and still staged. - Fix what you need to, or switch branches.
- Run
git commitagain when you're ready.
Tip: Only do this to commits you haven't pushed. Don't swap in --hard: that throws the changes away.
Try it now: In a test repo, make a commit, run
git reset --soft HEAD~1, then git status to see your changes waiting to be committed.Next crumbs
- 💻 Terminal & gitFix your last commit with --amendForgot a file or made a typo in the message? Fold the fix into the commit you just made.
- 💻 Terminal & gitThrow away changes to one file with git restoreMade a mess of one file? Put it back the way it was at your last commit or
git add, without touching anything else. - 💻 Terminal & gitJump back with cd -Toggle between your last two directories, like Alt+Tab for folders.