Undoing commits
Last updated
There are two ways to undo a commit. git revert adds a new commit that undoes it, which is safe for history others have. git reset moves your branch back, which is tidy for history only you have. In this lesson you'll use both, and the three kinds of reset.
By the end of this lesson you can
- Undo a commit with git revert
- Move a branch back with git reset --soft, --mixed and --hard, and say where the changes end up
- Choose revert for shared history and reset for your own
Revert: undo with a new commit
git revert <commit> works out the opposite of that commit's changes and makes a new commit with them. The old commit stays in history, and so does the fact that it was undone.
git revertSafe: only adds or reads; nothing is lost: it only adds a commit. That makes it the right undo for history others already have: nobody's copy is contradicted, they just get one more commit.
Git names the commit Revert "…" after the original. In a real terminal it opens your editor so you can add why; --no-edit skips that. The sandbox always uses Git's message.
Reset: move the branch back
git reset <commit> moves your branch (and HEAD) back to an earlier commit, as if the later ones were never made. The question is what happens to their changes. That depends on the mode:
| Mode | Branch moves back | Staging area | Working folder | Badge |
|---|---|---|---|---|
--soft | yes | keeps the changes, staged | keeps them | Careful: changes files or history, but Git can usually get it back |
--mixed (the default) | yes | reset to match | keeps them, unstaged | Careful: changes files or history, but Git can usually get it back |
--hard | yes | reset to match | reset to match: changes gone | Can lose work: can throw away work Git never saved, or rewrite shared history |
$ git log --oneline -2 67e435d (HEAD -> main) Cover the leche flan with foil ba04ca2 Add lumpiang shanghai recipe
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
--soft is how you "uncommit" to redo a commit differently. --mixed is the same with the changes unstaged. --hard is for when you really want the commits and their changes gone, and even then, lesson 2.7 shows the dashed commits can still be found for a while.
Try both
- Undo Joaquin’s sinigang commit with
git revert(it’sHEAD~3) - Undo the revert too, by moving main back one commit with
git reset --soft HEAD~1
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
You are in ~/kusina, which has ten commits from two weeks of work.
Commit graph
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
After the soft reset, the revert's changes are staged. git restore --staged sinigang.md and git restore sinigang.md would clear them, or you could commit them again.
Which undo?
The rule: if anyone else might have the commit, revert. If only you have it, reset is fine. Sort each situation.
0 of 6 placed right
A recipe change pushed last week turns out wrong. Carla and Joaquin both have it.
You just made three small commits on your own computer and want them as one.
You tried an idea in two unshared commits and want it all gone.
You edited sinigang.md, didn’t commit, and want it back as it was.
Your last commit, not pushed, should have been two commits.
A commit from a month ago, long since shared, added a recipe Lola Remy wants removed.
Spot the trap: reset --hard to "undo a commit"
Ana’s last commit has the right recipe change but the wrong message. Someone told her “to undo a commit, use git reset --hard HEAD~1”.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-revert: recording a new commit that reverses an earlier one
- Git documentation: git-reset: --soft, --mixed and --hard
- Pro Git: Reset Demystified: the three trees and what each reset mode moves
- Oh Shit, Git!?! (Katie Sylor-Miller): plain-language fixes for common mistakes, credited as inspiration