Looking back and undoing · lesson 5 of 7
Commands and ideas in this lesson
Agit reverta new commit that undoes an old one
Bgit reset --softmove back; keep changes staged
Cgit reset (--mixed)move back; keep changes unstaged
Dgit reset --hardmove back; throw the changes away

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:

ModeBranch moves backStaging areaWorking folderBadge
--softyeskeeps the changes, stagedkeeps themCareful: changes files or history, but Git can usually get it back
--mixed (the default)yesreset to matchkeeps them, unstagedCareful: changes files or history, but Git can usually get it back
--hardyesreset to matchreset to match: changes goneCan 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

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe
1 of 4 The newest commit, “Cover the leche flan with foil”, changed one line. Watch where that change goes with each kind of reset.

--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

Git sandbox
  1. Undo Joaquin’s sinigang commit with git revert (it’s HEAD~3)
  2. 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

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add 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

  1. A recipe change pushed last week turns out wrong. Carla and Joaquin both have it.

  2. You just made three small commits on your own computer and want them as one.

  3. You tried an idea in two unshared commits and want it all gone.

  4. You edited sinigang.md, didn’t commit, and want it back as it was.

  5. Your last commit, not pushed, should have been two commits.

  6. 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"

Spot the trapAna

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

After git reset --soft HEAD~1, where are the last commit’s changes?
A wrong commit has been pushed and Carla has pulled it. How should Ana undo it?
Which reset throws away uncommitted changes in your files?

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.

All sources for Simple Git