How to Undo a Pushed Commit with git revert
If you pushed a wrong change to a shared branch and want to take it back, use git revert. git revert creates a new commit with the opposite changes of the commit you specify. Because it does not rewrite history, you can push it with a normal git push without --force. Besides the basic operation of reverting one commit, I checked how to resolve a conflict and how to use -m 1 when reverting a merge commit, using the actual output from my test environment.
The test environment is Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>). To make the output the same every time, I fixed the commit dates with the environment variables GIT_AUTHOR_DATE and GIT_COMMITTER_DATE. The date shown on the Date: line in the output is the date set by these environment variables. The push target was a bare repository (../remote.git) created in the same environment. I did not test pushing to hosting services such as GitHub, and I did not test on Windows or macOS.
Check the commit you want to revert
The scenario: I pushed a change that set the shipping fee to 0 yen (0100cda), and then I realized 0 yen was a mistake. After that, I also pushed a commit that added an announcement.
$ git log --oneline
96ffc30 お知らせを追加
0100cda 送料を0円に変更
c93c615 料金表を追加
a26abbe 最初のコミット
$ git status -sb
## main...origin/main
$ cat price.txt
送料: 0円
手数料: 200円
You can check the contents of the commit you want to undo with git show. HEAD~1 refers to the commit just before the current one (here, 0100cda).
$ git show --format="%h %s" HEAD~1
0100cda 送料を0円に変更
diff --git a/price.txt b/price.txt
index 67ef22c..eb91650 100644
--- a/price.txt
+++ b/price.txt
@@ -1,2 +1,2 @@
-送料: 500円
+送料: 0円
手数料: 200円
Create a commit with the opposite changes using git revert
Pass the commit you want to undo to git revert. Specifying it by hash (0100cda) or by HEAD~1 gives the same result.
When you run it from a terminal, an editor opens for you to write the commit message (official git revert reference). To see what is passed to the editor, I used cat in place of the editor to display the contents, then treated it as if I had closed the editor without changing anything.
$ GIT_EDITOR=cat git revert --edit HEAD~1
Revert "送料を0円に変更"
This reverts commit 0100cda3fa8249e1082950d1fecff512ccc12920.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# Your branch is up to date with 'origin/main'.
#
# Changes to be committed:
# modified: price.txt
#
[main cd150f2] Revert "送料を0円に変更"
1 file changed, 1 insertion(+), 1 deletion(-)
The message automatically includes Revert "original message" and the full hash of the reverted commit. In practice, it is helpful to add the reason for the revert to this message so that you can understand it later when reading the history. The official reference also strongly recommends writing the reason. If you want to create the commit with this automatic message without opening an editor, add --no-edit.
$ git log --oneline
cd150f2 Revert "送料を0円に変更"
96ffc30 お知らせを追加
0100cda 送料を0円に変更
c93c615 料金表を追加
a26abbe 最初のコミット
$ cat price.txt
送料: 500円
手数料: 200円
0100cda stays in the history, and cd150f2 was added on top of it. The shipping fee in price.txt is back to 500 yen. The change from the later announcement commit (96ffc30) remains as is.
Looking at it with git show, cd150f2 has exactly the opposite diff of 0100cda.
$ git show HEAD
commit cd150f2bd4f32d9805f1ea6446d04a67095ae285
Author: Neko <neko@example.com>
Date: Wed Nov 11 10:40:00 2026 +0900
Revert "送料を0円に変更"
This reverts commit 0100cda3fa8249e1082950d1fecff512ccc12920.
diff --git a/price.txt b/price.txt
index eb91650..67ef22c 100644
--- a/price.txt
+++ b/price.txt
@@ -1,2 +1,2 @@
-送料: 0円
+送料: 500円
手数料: 200円

The push succeeds without –force
After the revert, git status shows ahead 1, meaning there is one commit in your local branch that is newer than the remote. There is no behind.
$ git status -sb
## main...origin/main [ahead 1]
$ git push origin main
To ../remote.git
96ffc30..cd150f2 main -> main
$ git status -sb
## main...origin/main
The output shows 96ffc30..cd150f2, and the remote main simply moved forward. Because the remote history is not rewritten, you do not need a force push (--force). The Pro Git book's section on advanced merging (Japanese edition) also says that commits others have already pulled should not be undone with reset, and it introduces revert as the way to create a new commit that cancels them.
If the same lines changed after the commit you undo, you get a conflict
Sometimes the same line changes again after the commit you want to undo. Here I revert fc18cf2, which added the line "free shipping in November". However, the next commit f1860cf rewrote the same line to "free shipping in November and December".
$ git log --oneline -3
f1860cf キャンペーンを12月まで延長
fc18cf2 キャンペーンのお知らせを追加
cd150f2 Revert "送料を0円に変更"
$ git revert --no-edit HEAD~1
Auto-merging news.txt
CONFLICT (content): Merge conflict in news.txt
error: could not revert fc18cf2... キャンペーンのお知らせを追加
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".
To undo fc18cf2, you would remove the line "free shipping in November", but the file now contains "free shipping in November and December". Git cannot decide which one should remain, so it stops with a conflict. The file contains markers that show the conflict.
$ cat news.txt
営業時間: 10時から18時
<<<<<<< HEAD
11月と12月は送料無料
=======
>>>>>>> parent of fc18cf2 (キャンペーンのお知らせを追加)
The part from <<<<<<< HEAD to ======= is the current content, and the part below ======= is the content from the parent of fc18cf2 (the state before that line existed).
To stop, use git revert –abort
If you want to stop undoing the commit for now, git revert --abort returns you to the state before the revert started (official git revert reference).
$ git revert --abort
$ git status -sb
## main...origin/main [ahead 2]
$ cat news.txt
営業時間: 10時から18時
11月と12月は送料無料
$ git log --oneline -1
f1860cf キャンペーンを12月まで延長
The file and the tip of the branch (f1860cf) are both back to how they were before the revert.
To continue after fixing, use git revert –continue
I ran the revert again to produce the conflict, then fixed the file to the state I wanted. Here I decided to remove the campaign line entirely. After running git add on the fixed file, I ran git revert --continue.
$ printf "営業時間: 10時から18時\n" > news.txt
$ git add news.txt
$ git revert --continue --no-edit
[main 1b41740] Revert "キャンペーンのお知らせを追加"
1 file changed, 1 deletion(-)
$ git log --oneline -4
1b41740 Revert "キャンペーンのお知らせを追加"
f1860cf キャンペーンを12月まで延長
fc18cf2 キャンペーンのお知らせを追加
cd150f2 Revert "送料を0円に変更"
Because I added --no-edit to --continue, the editor did not open, and the commit was made with the automatic message.
To revert a merge commit, add -m 1
The scenario: I merged the branch feature into main and pushed it, and now I want to undo that whole merge.
$ git log --oneline --graph -5
* f61bada カート機能をマージ
|\
| * de543fa 数量の説明を追加
| * fddd77d カートのボタンを追加
|/
* 1b41740 Revert "キャンペーンのお知らせを追加"
* f1860cf キャンペーンを12月まで延長
If you pass the merge commit to git revert as is, it stops with an error.
$ git revert --no-edit HEAD
error: commit f61badac041e3e792a04c6d60406772178465f58 is a merge but no -m option was given.
fatal: revert failed
A merge commit has two parents, and Git cannot decide which parent's side of the changes to undo (the official reference's explanation of -m). You can check the order of the parents with git cat-file -p.
$ git cat-file -p HEAD | grep ^parent
parent 1b417408d673c45fc922ebfd40444ed4c79b3714
parent de543fa6677c41c722efae0256175cef059f67c2
$ git log --oneline -1 HEAD^1
1b41740 Revert "キャンペーンのお知らせを追加"
$ git log --oneline -1 HEAD^2
de543fa 数量の説明を追加
The first parent is main before the merge (1b41740), and the second parent is the tip of feature (de543fa). I want to undo the changes brought in from feature and keep the main side, so I add -m 1.
$ git revert --no-edit -m 1 HEAD
[main bb445c0] Revert "カート機能をマージ"
Date: Wed Nov 11 12:30:00 2026 +0900
1 file changed, 2 deletions(-)
delete mode 100644 cart.html
$ ls
README.md
news.txt
price.txt
The cart.html file that was added on feature is gone. The message also records which parent was used as the base for the revert.
$ git show --stat --format="%h %s%n%n%b" HEAD
bb445c0 Revert "カート機能をマージ"
This reverts commit f61badac041e3e792a04c6d60406772178465f58, reversing
changes made to 1b417408d673c45fc922ebfd40444ed4c79b3714.
cart.html | 2 --
1 file changed, 2 deletions(-)
A reverted merge does not come back when you merge again
After reverting a merge, merging the same feature branch again does not bring the reverted changes back.
$ git merge feature
Already up to date.
$ ls
README.md
news.txt
price.txt
This is because the commits on feature are already treated as part of the main history (merged). The official reference also says that reverting a merge declares the changes brought in by that merge unneeded, and that commits that are ancestors of the reverted merge will not be brought in by a later merge. Even if you add new commits to feature and then merge, only the commits added to feature after the reverted merge are brought in (Git's documentation on how to revert a faulty merge).
If you fix the branch that was reverted (here, feature) and want to merge it again, first revert the revert commit to restore the changes, and then do the new merge. Pro Git also says this order is the best approach.
Revert the revert to restore it
If you decide to stop undoing the merge and want the merged content back, revert the revert commit (bb445c0).
$ git revert --no-edit HEAD
[main ca0c2ca] Reapply "カート機能をマージ"
Date: Wed Nov 11 12:50:00 2026 +0900
1 file changed, 2 insertions(+)
create mode 100644 cart.html
$ git log --oneline -3
ca0c2ca Reapply "カート機能をマージ"
bb445c0 Revert "カート機能をマージ"
f61bada カート機能をマージ
$ ls
README.md
cart.html
news.txt
price.txt
$ git push origin main
To ../remote.git
f61bada..ca0c2ca main -> main
cart.html is back. In the test environment with Git 2.43.0, reverting a revert commit produced the message Reapply "カート機能をマージ" instead of Revert "Revert ...". All three commits, from the merge through the revert to the restore, remain in the history, and the push went through with a normal git push.




