Git Revert

Updated · 10 min read
easyGit 2.53gitgit-revertundo-commitinterview

When a bank makes a wrong payment into your account, it doesn't erase the entry from your statement. It adds a new entry that takes the money back. The statement stays honest: anyone reading it sees both the mistake and the correction.

git revert undoes a commit the same way. It doesn't remove the commit from history. It adds a new commit that does the exact opposite: every line the old commit added is removed, and every line it removed is put back. Because nothing in the existing history changes, it is the safe way to undo a commit that you have already pushed and that your teammates already have.

In this lesson, you will revert the newest commit and an older one, read the message Git writes for a revert, and undo a revert itself.

The Starting Point

Navika's project has four commits, all pushed and shared with the team. The newest one added a hostel fee that the school office hasn't approved yet:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add project README"
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fees"
echo "class FeeCalculator {}" > FeeCalculator.java
git add FeeCalculator.java
git commit --quiet --author="Srihitha Anamaneni <srihitha@example.com>" -m "Add the fee calculator"
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git log --oneline
Output
f01e719 (HEAD -> main) Add the hostel fee
af6dd16 Add the fee calculator
4009644 Add the term fees
dd34d8a Add project README

Revert the Newest Commit

Because the commits are shared, a reset is out of the question. Navika reverts the hostel fee commit:

Terminal
git revert --no-edit HEAD
git log --oneline
cat fees.txt
Output
[main 0777ccd] Revert "Add the hostel fee"
 Date: Mon Jan 5 10:17:00 2026 -0600
 1 file changed, 1 deletion(-)
0777ccd (HEAD -> main) Revert "Add the hostel fee"
f01e719 Add the hostel fee
af6dd16 Add the fee calculator
4009644 Add the term fees
dd34d8a Add project README
Term 1 fee: $12,000

How it works

  1. Git worked out the opposite of "Add the hostel fee": remove the hostel line from fees.txt.
  2. It applied that change and committed it as a new commit, Revert "Add the hostel fee".
  3. The original commit is still in the history, below the revert. Anyone can see that the fee was added and then taken back.

Without --no-edit, Git opens your editor with the message it suggests, so you can add the reason. That is a good habit for real projects: say why the commit was reverted.

Here is the full message Git wrote:

Terminal
git show -s HEAD
Output
commit 0777ccdc0f85e4d5ddef1130a093eca78abd6da2 (HEAD -> main)
Author: Navika Anamaneni <navika@example.com>
Date:   Mon Jan 5 10:17:00 2026 -0600
 
    Revert "Add the hostel fee"
 
    This reverts commit f01e71940e69c8eccc7a39c9475347b08e6e798d.

The body names the full ID of the reverted commit, so the two can always be connected.

Revert an Older Commit

A revert doesn't have to be the newest commit. Any commit can be reverted, as long as later commits didn't change the same lines. The team decides to remove the empty FeeCalculator class for now:

Terminal
git revert --no-edit af6dd16
git log --oneline
ls
Output
[main 04b6d3a] Revert "Add the fee calculator"
 Date: Mon Jan 5 10:23:00 2026 -0600
 1 file changed, 1 deletion(-)
 delete mode 100644 FeeCalculator.java
04b6d3a (HEAD -> main) Revert "Add the fee calculator"
0777ccd Revert "Add the hostel fee"
f01e719 Add the hostel fee
af6dd16 Add the fee calculator
4009644 Add the term fees
dd34d8a Add project README
README.md
fees.txt

Commit af6dd16 added FeeCalculator.java, so its opposite deletes the file. The commits made after it are untouched.

Syntax

Terminal
git revert [--no-edit] [--no-commit] <commit>...
OptionIt is used to
git revert <commit>Make a new commit that reverses <commit>, after letting you edit the message
--no-editUse Git's message without opening the editor
--no-commit or -nApply the reverse changes to your files and staging area, but don't commit yet
git revert <a>..<b>Revert a range of commits, one revert commit each, newest first
-m 1Revert a merge commit, keeping the first parent's side
--abortGive up a revert that stopped with a conflict
--continueFinish a revert after you resolved its conflict
Reverting adds commits instead of removing them

Undo a Revert

The office approves the hostel fee after all. A revert is a commit like any other, so reverting it brings the change back:

Terminal
git revert --no-edit HEAD~1
git log --oneline
cat fees.txt
Output
[main 451bd92] Reapply "Add the hostel fee"
 Date: Mon Jan 5 10:27:00 2026 -0600
 1 file changed, 1 insertion(+)
451bd92 (HEAD -> main) Reapply "Add the hostel fee"
04b6d3a Revert "Add the fee calculator"
0777ccd Revert "Add the hostel fee"
f01e719 Add the hostel fee
af6dd16 Add the fee calculator
4009644 Add the term fees
dd34d8a Add project README
Term 1 fee: $12,000
Hostel fee: $9,000

HEAD~1 was the first revert. Reverting it applies the hostel line again. The message even says it: Reapply "Add the hostel fee".

When a Revert Hits a Conflict

If later commits changed the same lines as the commit you revert, Git can't work out the opposite on its own. It stops and marks the conflict in the file, just like a merge conflict. You then have two choices:

  • Fix the file, stage it with git add, and run git revert --continue.
  • Give up with git revert --abort, which puts everything back as it was before the revert.

You will see one in the practice at the end of this lesson. Resolving conflicts has its own lessons, with merging.

Common Mistakes

Using git reset on pushed commits. Teammates already have the commits. Use git revert, which doesn't rewrite anything.

Expecting the original commit to disappear. It stays in the history, which is the whole point. git log shows both.

Leaving Git's message unchanged on a real project. Revert "..." says what was undone but not why. Add a body with the reason.

Reverting a merge without -m. A merge has two parents, and Git needs to know which side to keep. git revert -m 1 <merge> keeps the branch that was merged into. Merges are explained in a later module.

Reverting the same commit twice. The second revert has nothing to undo, or undoes your fix in an unexpected way. Check git log for an existing revert first.

Under the Hood

A revert is a three-way calculation. For commit C with parent P, Git takes the difference from C back to P and applies it to your current HEAD. That is the same machinery a merge and git cherry-pick use, which is why a revert can have conflicts, and why reverting works even after many later commits, as long as they changed different lines.

After a revert, the content is the same as if the commit had never been made, but the history is different. That matters later: if a reverted branch is merged again, Git sees its commits as already merged and doesn't bring them back. The fix is to revert the revert, as you did above.

Interview Corner

What does git revert do?

It creates a new commit that applies the inverse of a given commit's changes. The original commit stays in the history. Because the history isn't rewritten, it is safe to use on commits that have been pushed and shared.

What is the difference between git revert and git reset?

git reset moves the branch back, removing commits from its history, which rewrites history and is only safe for unpushed commits. git revert keeps all commits and adds a new one that undoes the change, which is safe for shared branches.

Can you revert a commit that is not the latest one?

Yes. git revert <commit> works on any commit. If later commits changed the same lines, Git stops with a conflict that you resolve and then run git revert --continue, or cancel with git revert --abort.

How do you revert a merge commit?

git revert -m 1 <merge-commit>. -m 1 tells Git to treat the first parent (the branch that was merged into) as the side to keep, and undoes what the merged branch brought in.

How do you undo a revert?

Revert the revert commit: git revert <revert-commit>. Git names the new commit Reapply "...".

Practice

Challenge: revert the first fee. In the same repository, revert the commit "Add the term fees" (find its ID with git log --oneline).

What does Git print? Before you open the solution, look at what "Add the term fees" did with git show --stat 4009644, and think about which later commits changed the same file.

Show solution
Terminal
git revert --no-edit 4009644
Output
CONFLICT (modify/delete): fees.txt deleted in parent of 4009644 (Add the term fees) and modified in HEAD.  Version HEAD of fees.txt left in tree.
error: could not revert 4009644... Add the term fees
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".
hint: Disable this message with "git config set advice.mergeConflict false"

The revert stops with a conflict. "Add the term fees" created fees.txt, so its opposite is to delete the file. But later commits changed fees.txt (the hostel fee lines), and Git won't delete a file that holds changes it was not asked to undo. It calls this a modify/delete conflict: one side deleted the file, the other modified it. No commit was made, and Git lists your choices in the hints.

The fee list should stay, so cancel the revert:

Terminal
git revert --abort
git status -s
git log --oneline -1
Output
451bd92 (HEAD -> main) Reapply "Add the hostel fee"

--abort put everything back. The status is clean, and the newest commit is still the reapplied hostel fee.

Checkpoint. In a practice repository, make four commits that each change a different file. Revert the second one, then revert the revert, and run git log --oneline after each step. Then make two commits that change the same line, revert the older one, and use git revert --abort when it stops.

Quick check: Git revert

4 questions
1What does git revert <commit> create?
2Why is git revert safe on pushed commits?
3Which option skips the editor and uses Git's message?
4A revert stopped with a conflict and you want to cancel it. What do you run?

Key Takeaways

Quick reference

CommandIt is used to
git revert HEADUndo the newest commit with a new commit
git revert <commit>Undo any commit with a new commit
git revert --no-edit <commit>Revert without opening the editor
git revert --no-commit <a> <b>Stage several reverts to commit as one
git revert -m 1 <merge>Revert a merge commit
git revert --continue / --abortFinish or cancel a revert with a conflict

Was this tutorial helpful?

Lesson 25 of 106