git undo last commit
Ooops, I did it again. Here’s how to undo your last git commit.
If you did not
git push, then you have a easy choice on how much you want to undo. If you did push, it’s more work but can be done.
- Keep changes, just modify the commit message
- Undo commit, but keep changes
- Undo the commit, undo the changes
- Undo a push, and the commit
How do I just change the last commit message?
If you didn’t push, and just need to change the last commit message, use
git commit --amend -m "New commit message"
How do I undo the last commit and but keep the changes locally
This is useful when you forgot to add a file or what to make a few more edits. If you did not push, then, this makes it so it never happened and restore everything just as you never typed
git reset HEAD~1
Undo the commit and discard the changes
Same as above but also reverts any changes to to file as if you never did the edits at all.
git reset --hard HEAD~1
See Stack Overflow for more details.
Undo a push and the commit
The cute answer is “keep going and don’t try to revert.” That’s not appropriate in all occasions, so if you need to undo a push and a commit, check out see this Stack Overflow thread until I write up something.