Amend and revert

Topic 3 of 4 in Undoing changes6 min readBeginnerFree course

Overview

The previous lesson’s tools move your branch backwards. This lesson adds two more precise ones:

  • git commit --amend — fix the commit you just made.
  • git revert — undo any commit, at any point in history, without rewriting history.

Amending the last commit

You commit, and immediately notice a typo in the message, or a file you forgot to add. Instead of making a “fix” commit, amend:

Terminal
git add signup.css                         # stage anything you forgot
git commit --amend -m "Add signup form"    # replace the last commit

Without -m, Git opens your editor with the old message ready to edit. To keep the message and only add the staged changes:

Terminal
git commit --amend --no-edit

You can also use --amend to fix the author after correcting your config: git commit --amend --reset-author --no-edit.

What amend really does

Commits can’t be changed — so --amend creates a new commit with the combined content and message, and moves your branch to it. The old commit is left behind (the reflog still remembers it).

That has the same consequence as reset: don’t amend a commit you’ve already pushed. Others may have built on the old commit; your new one has a different ID, and the histories no longer match. (On your own feature branch, where nobody else works, teams sometimes amend and force-push deliberately — Pushing and pulling shows how to do that safely.)

Reverting a commit

git revert undoes a commit by creating a new commit that does the opposite: lines it added are removed, lines it removed are added back.

Terminal
git revert 7d02b4e

Git opens your editor with a message like:

Text
Revert "Change prices"

This reverts commit 7d02b4e1c3f0a9e8d7c6b5a4f3e2d1c0b9a8f7e6.

Save and close (or use --no-edit to accept it), and the revert commit is added to the end of your branch.

Undoing a commit: revert or reset? On the left, git revert c9 is safe for shared history: after a1, 7d and c9 comes a new commit !c9 that undoes c9, and main points to it; history only grows and teammates are unaffected. On the right, git reset –hard 7d is local only: main moves back to 7d and c9 is dropped; this rewrites history and breaks other people’s copies. Already pushed? Use revert. Only on your machine? reset (or amend) is fine.

Why revert instead of reset for shared work?

  • History only grows. Nobody else’s copy is invalidated. They just pull the revert like any other commit.
  • It’s documented. The log shows that the change was made, and that it was undone — and the message should say why.
  • It works on any commit, not just the latest. Git computes the opposite of that one commit and applies it to the current state.

Reverting conflicts

If later commits changed the same lines, Git can’t reverse the old change automatically, and you get a conflict — the same kind you’ll resolve in Resolving merge conflicts. Fix the files, then:

Terminal
git add prices.json
git revert --continue      # or: git revert --abort to give up

Reverting several commits

Terminal
git revert --no-edit HEAD~3..HEAD     # revert the last three commits, one revert commit each
git revert --no-commit HEAD~3..HEAD   # stage all the reversals, then make one commit yourself
git commit -m "Revert the search redesign"

Reverting a revert

Changed your mind? A revert is just a commit, so you can revert it, which brings the original change back:

Terminal
git revert <id-of-the-revert-commit>

Reverting a merge

Reverting a merge commit needs one extra piece of information: which parent is the “mainline” to keep. Usually that’s the first parent, the branch you merged into:

Terminal
git revert -m 1 <merge-commit>

(Merges are explained in the next chapter. Reverting merges has some subtleties if you later want to merge the same branch again — see git help revert.)

Which undo should I use?

Situation Use
Typo in the last commit’s message, not pushed git commit --amend
Forgot a file in the last commit, not pushed git add + git commit --amend --no-edit
Last few commits are local and wrong git reset (soft, mixed or hard)
A commit is already pushed / shared git revert
A commit far back in history broke something git revert

Rule of thumb: private history can be rewritten (amend, reset); public history should only grow (revert).

Exercise

Fix a commit and undo a shared one

Two mistakes to fix. (1) Your last commit, “Add sigup form”, has a typo in the message and forgot to include signup.css. Fix both with one amend, so the history still has one commit for the signup form. (2) A commit called “Change prices” was pushed days ago and broke the shop. Undo it without rewriting history, so a new commit appears that reverses it. Check that the price file is back to its earlier content and that the log contains both the original commit and the revert.

Terminal
mkdir signup && cd signup && git init -b main
echo '{"tea": 3}' > prices.json && git add . && git commit -qm "Add prices"
echo '{"tea": 30}' > prices.json && git commit -qam "Change prices"
echo "# Shop" > README.md && git add . && git commit -qm "Add README"
echo "<form>…</form>" > signup.html && git add signup.html && git commit -qm "Add sigup form"
echo "form { margin: 1rem; }" > signup.css     # forgot this one

# 1. Fix the last commit: add signup.css and correct the message

# 2. Undo "Change prices" with a new commit

Summary

  • git commit --amend replaces the last commit with a new one — add forgotten files or fix the message.
  • --amend --no-edit keeps the message and just adds the staged changes.
  • Amending creates a new commit ID, so only amend commits you haven’t pushed.
  • git revert <commit> adds a new commit that applies the opposite of an old one — history is never rewritten.
  • Revert is the safe way to undo shared commits; a revert can itself be reverted to bring the change back.