Skip to content
BENIBIX Learn

Undo a Git Commit

Level 3 · Undo and set aside

Three ways to take back a commit: revert, reset and amend. Which one is safe on a branch others have, and which one can lose your work for good.

Course contentsLesson 7 of 13

After this lesson you can

  • Undo a shared commit with revert
  • Move a branch back with reset
  • Fix your last commit with amend

13 mincovers 3 glossary entries

Worth reading first: Git Rebase and Cherry-Pick

Which undo to use: revert, reset or amend

Git has three main ways to undo a commit. Revert adds a new commit that does the opposite. Reset moves the branch back, so later commits drop out. Amend swaps your last commit for a fixed one. Only revert is safe on a branch other people already have, because it is the only one that adds rather than rewrites.

Each one fits a different moment. A commit pushed yesterday that broke the site wants a revert. Three messy commits you made this morning and never shared can take a reset. A typo in the message you wrote a minute ago wants an amend.

Git revert takes a commit's hash and writes a new commit that undoes it. If the old commit added a line, the new one takes it out. The old commit stays in the history, so anyone can see what happened and why. Nothing is deleted, which is what makes it safe to share.

Git reset moves your branch label back to an earlier commit, and it has three modes. Soft keeps the undone changes staged. Mixed, the default, keeps them in your files but not staged. Hard throws them away, along with any changes you never committed.

Git reset --soft HEAD~1 is a handy trick. HEAD~1 means one commit before where you are. That commit disappears, but its changes stay staged, so you can commit them again with a better message, or split them up.

Git commit --amend replaces your last commit with a new one. You can change the message, add a file you forgot, or both. Git makes a new commit with a new hash, and the old one drops out of the branch. To add a forgotten file, stage it first, then amend.

Reset and amend both rewrite history. On your own branch that nobody else has, that is fine. Others may have pulled the branch, though. Their copies still hold the commits you removed, and the next push or pull turns into a mess. For anything already shared, use revert.

The costly mistake is to run git reset --hard without a second thought. The commits it takes away can often be found again for a while. Changes you never committed are gone for good. Run git status first, and stash or commit anything you might want back.

The three undo commands

CommandWhat it doesSafe once shared?
git revertAdds a commit that does the oppositeYes
git resetMoves the branch back to an older commitNo
git commit --amendSwaps the last commit for a fixed oneNo

Which one to pick

  1. 1

    Revert

    When the bad commit is already pushed, and other people may have it.

  2. 2

    Reset

    When the commits are only on your computer, and you want to redo them.

  3. 3

    Amend

    When only your last commit is wrong, and you have not pushed it yet.

console
$ git log --oneline -2
e963df2 Add a search page
cb0fe9d Add footer
$ git reset --soft HEAD~1
$ git status --short
A  search.html
The commit is gone from the log, but its change is still staged and ready to commit again.
Revert adds a commit. Reset and amend take one away, which is why they are only for work you have not shared.

Worked example

Jack takes back a bad price

On Monday Jack pushes a commit that shows every price 10 times too high. Two teammates have already pulled it.

He runs git revert on that commit's hash and pushes. A new commit puts the prices back, his teammates pull it like any other change, and the history shows both.

The words for this lesson3

Check yourself

Which undo is safe on a branch my team has already pulled?

Finished this lesson?

Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.