TExcelTAD
Home › Git › Git Revert

Git Revert

git revert <commit> undoes a commit by creating a new commit that applies the opposite change. History is not rewritten: the bad commit stays, followed by its reversal. That makes revert the safe way to undo changes that have already been pushed and shared.

before:  A --- B --- C (bad)            main
after:   A --- B --- C --- C' (revert of C)   main

Reverting the latest commit

bash · try it yourself
mkdir shop && cd shop && git init -q
printf 'shipping=4.99\ndiscount=0\n' > settings.ini && git add . && git commit -q -m "Add shop settings"
sed -i 's/discount=0/discount=90/' settings.ini && git commit -q -am "Set launch discount"
git revert --no-edit HEAD
git log --oneline
cat settings.ini
[main b70a4b5] Revert "Set launch discount"
 Date: Thu Jan 15 10:00:00 2026 +0000
 1 file changed, 1 insertion(+), 1 deletion(-)
b70a4b5 Revert "Set launch discount"
6da0fab Set launch discount
d68f68c Add shop settings
shipping=4.99
discount=0

Without --no-edit, Git opens your editor with the message Revert "Set launch discount" and a line saying which commit it reverts, so you can explain why it was undone (recommended in real projects).

Reverting an older commit

You can revert any commit, not just the last one. Git only undoes that commit's change; later commits stay:

bash · try it yourself
mkdir shop && cd shop && git init -q
echo "shipping=4.99" > shipping.ini && git add . && git commit -q -m "Add shipping"
echo "currency=EUR" > currency.ini && git add . && git commit -q -m "Add currency"
echo "tax=20" > tax.ini && git add . && git commit -q -m "Add tax"
git log --oneline
git revert --no-edit HEAD~1 >/dev/null
git log --oneline -1
ls
160679b Add tax
8f3fd91 Add currency
f1ba74d Add shipping
14cffa4 Revert "Add currency"
shipping.ini
tax.ini

The currency file is gone, while the newer tax file stays. If later commits changed the same (or neighbouring) lines, the revert can conflict; resolve it like a merge, then git revert --continue.

Reverting several commits

Give a range to revert several commits, newest first. Remember that A..B excludes A itself, so the three newest commits are HEAD~3..HEAD. With --no-commit (-n) the reversals are only staged, so you can make one commit that undoes them all:

bash · try it yourself
mkdir shop && cd shop && git init -q
echo "base" > log.txt && git add . && git commit -q -m "Base"
for step in one two three; do echo "$step" >> log.txt; git commit -q -am "Step $step"; done
git revert --no-commit HEAD~3..HEAD
git status -s
git commit -q -m "Revert steps one to three: feature postponed"
git log --oneline
cat log.txt
M  log.txt
843b91c Revert steps one to three: feature postponed
65d3a32 Step three
5e33f8a Step two
056cac0 Step one
8e315ab Base
base

Reverting a merge

A merge commit has two parents, so Git needs to know which side is the "mainline" to keep. -m 1 means "keep the first parent (the branch that was merged into), undo what the other branch brought":

bash · try it yourself
mkdir shop && cd shop && git init -q
echo "home" > index.html && git add . && git commit -q -m "Home page"
git switch -q -c blog && echo "blog" > blog.html && git add . && git commit -q -m "Add blog"
git switch -q main && echo "footer" >> index.html && git commit -q -am "Add footer"
git merge -q --no-edit blog
ls
git revert --no-edit -m 1 HEAD
ls
git log --oneline --graph
blog.html
index.html
[main 697f51c] Revert "Merge branch 'blog'"
 Date: Thu Jan 15 10:00:00 2026 +0000
 1 file changed, 1 deletion(-)
 delete mode 100644 blog.html
index.html
* 697f51c Revert "Merge branch 'blog'"
*   0c4178f Merge branch 'blog'
|\
| * ea0ada3 Add blog
* | cedabbc Add footer
|/
* 9f3b8b0 Home page

Warning: After reverting a merge, merging the same branch again brings nothing, because Git considers its commits already merged. To re-apply it later, revert the revert commit.

revert vs reset

git revert git reset (next lesson)
How adds a new, opposite commit moves the branch pointer back
History preserved commits removed from the branch
Safe on pushed/shared branches yes no (needs a force push)
Typical use undo a change on main tidy up your own unpushed commits

Test yourself

What does git revert HEAD do?

  1. Deletes the last commit
  2. Creates a new commit that undoes the last commit's changes
  3. Moves HEAD back one commit
  4. Restores the working folder only

Why is revert preferred for undoing commits on a shared branch?

  1. It is faster
  2. It doesn't rewrite history, so teammates' copies stay valid
  3. It deletes the commit from the server
  4. It needs no commit message

What does -m 1 mean in git revert -m 1 <merge-commit>?

  1. Revert one file
  2. Keep parent 1 (the branch merged into) as mainline and undo the merged branch's changes
  3. Use message 1
  4. Revert only one commit of the branch