Expanding what I wrote in a comment

The general rule is that you should not rewrite (change) history that you have published, because somebody might have based their work on it. If you rewrite (change) history, you would make problems with merging their changes and with updating for them.

So the solution is to create a new commit which reverts changes that you want to get rid of. You can do this using git revert command.

You have the following situation:

A <-- B  <-- C <-- D                                  <-- master <-- HEAD

(arrows here refers to the direction of the pointer: the "parent" reference in the case of commits, the top commit in the case of branch head (branch ref), and the name of branch in the case of HEAD reference).

What you need to create is the following:

A <-- B  <-- C <-- D <-- [(BCD)-1]                   <-- master <-- HEAD

where [(BCD)^-1] means the commit that reverts changes in commits B, C, D. Mathematics tells us that (BCD)-1 = D-1 C-1 B-1, so you can get the required situation using the following commands:

$ git revert --no-commit D
$ git revert --no-commit C
$ git revert --no-commit B
$ git commit -m "the commit message for all of them"

Works for everything except merge commits.


Alternate solution would be to checkout contents of commit A, and commit this state. Also works with merge commits. Added files will not be deleted, however. If you have any local changes git stash them first:

$ git checkout -f A -- . # checkout that revision over the top of local files
$ git commit -a

Then you would have the following situation:

A <-- B  <-- C <-- D <-- A'                       <-- master <-- HEAD

The commit A' has the same contents as commit A, but is a different commit (commit message, parents, commit date).


Alternate solution by Jeff Ferland, modified by Charles Bailey builds upon the same idea, but uses git reset. Here it is slightly modified, this way WORKS FOR EVERYTHING:

$ git reset --hard A
$ git reset --soft D # (or ORIG_HEAD or @{1} [previous location of HEAD]), all of which are D
$ git commit
Answer from Jakub Narębski on Stack Overflow
Top answer
1 of 16
2243

Expanding what I wrote in a comment

The general rule is that you should not rewrite (change) history that you have published, because somebody might have based their work on it. If you rewrite (change) history, you would make problems with merging their changes and with updating for them.

So the solution is to create a new commit which reverts changes that you want to get rid of. You can do this using git revert command.

You have the following situation:

A <-- B  <-- C <-- D                                  <-- master <-- HEAD

(arrows here refers to the direction of the pointer: the "parent" reference in the case of commits, the top commit in the case of branch head (branch ref), and the name of branch in the case of HEAD reference).

What you need to create is the following:

A <-- B  <-- C <-- D <-- [(BCD)-1]                   <-- master <-- HEAD

where [(BCD)^-1] means the commit that reverts changes in commits B, C, D. Mathematics tells us that (BCD)-1 = D-1 C-1 B-1, so you can get the required situation using the following commands:

$ git revert --no-commit D
$ git revert --no-commit C
$ git revert --no-commit B
$ git commit -m "the commit message for all of them"

Works for everything except merge commits.


Alternate solution would be to checkout contents of commit A, and commit this state. Also works with merge commits. Added files will not be deleted, however. If you have any local changes git stash them first:

$ git checkout -f A -- . # checkout that revision over the top of local files
$ git commit -a

Then you would have the following situation:

A <-- B  <-- C <-- D <-- A'                       <-- master <-- HEAD

The commit A' has the same contents as commit A, but is a different commit (commit message, parents, commit date).


Alternate solution by Jeff Ferland, modified by Charles Bailey builds upon the same idea, but uses git reset. Here it is slightly modified, this way WORKS FOR EVERYTHING:

$ git reset --hard A
$ git reset --soft D # (or ORIG_HEAD or @{1} [previous location of HEAD]), all of which are D
$ git commit
2 of 16
821

Clean way which I found useful

git revert --no-commit HEAD~3..
git commit -m "your message regarding reverting the multiple commits"

This command reverts last 3 commits with only one commit.

Also doesn't rewrite history, so doesn't require a force push.

The .. helps create a range. Meaning HEAD~3.. is the same as HEAD~3..HEAD

🌐
GitHub
docs.github.com › en › desktop › managing-commits › reverting-a-commit-in-github-desktop
Reverting a commit in GitHub Desktop - GitHub Docs
When you revert to a previous commit, the revert is also a commit. The original commit also remains in the repository's history. ... When you revert multiple commits, it's best to revert in order from newest to oldest.
Discussions

Revert changes based on multiple commit selection
The feature request GitHub Desktop shows the diff when selecting multiple commits, it would be great if you could right click a selection of commits and reverse the entire net diff. Proposed solution Currenty, if you have to revert multi... More on github.com
🌐 github.com
0
August 24, 2023
git - How to revert multiple commits as part of a single commit - Stack Overflow
This is not a major problem, just something I want to know whether or not is possible. Let's say we have two commits, abcd123 and wxyz789, that occur at non-adjacent, separate places, far back in ... More on stackoverflow.com
🌐 stackoverflow.com
Revert a squashed merge
You can rebase or cherry-pick the old commits on top of the revert to be sure but I think it should work as is since the revert is for the squashed commits, not for the original ones. If you create a new pull request from a branch that has the old commits and the diff looks like expected it will be fine. If the diff is empty, try the rebase or cherry-pick approach. More on reddit.com
🌐 r/git
12
1
June 6, 2024
Best practices in rolling back PR's in production?
You can use git revert to make a new PR that undoes the problematic change. More on reddit.com
🌐 r/git
6
2
February 20, 2024
🌐
Better Stack
betterstack.com › community › questions › how-to-revert-multiple-commits
How Can I Revert Multiple Git Commits? | Better Stack Community
You'll need the hashes of the commits or a reference to a branch that includes them. Revert Commits: Use git revert to create new commits that undo the changes introduced by each of the specified commits:
🌐
GitHub
gist.github.com › rponte › 8a7c95232f469a4b6fd17dfedb8b90b4
Git: reverting a range of commits · GitHub
Git: reverting a range of commits · Raw · git-revert-multiple-commits.sh · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
LightNode
go.lightnode.com › tech › rollback-commit-github
How to Rollback Commits in GitHub: A Complete Developer's Guide
July 24, 2025 - Here are the most important commands for rolling back commits on GitHub: # Revert a single commit (safe for shared repos) git revert <commit-hash> # Revert multiple commits git revert <oldest-hash>..<newest-hash> # Revert without auto-committing ...
🌐
Pierian Training
pieriantraining.com › home › git tutorial: git revert multiple commits
Git Tutorial: Git Revert Multiple Commits - Pierian Training
April 28, 2023 - Instead, it creates a new commit that undoes the changes introduced by the original commit. In summary, reverting multiple commits in Git is straightforward with the `git revert` command.
🌐
GitHub
docs.github.com › en › desktop › managing-commits › undoing-a-commit-in-github-desktop
Undoing a commit in GitHub Desktop - GitHub Docs
You can undo multiple sequential commits up to a commit that has already been pushed to the remote repository by selecting a previous commit and using the "reset to commit" option.
Find elsewhere
🌐
GitHub
github.com › desktop › desktop › issues › 17278
Revert changes based on multiple commit selection · Issue #17278 · desktop/desktop
August 24, 2023 - GitHub Desktop shows the diff when ... if you have to revert multiple commits, you have to revert each commit individually and make sure you are going from newest to oldest....
Author   desktop
🌐
SheCanCode
shecancode.io › home › news & articles › how to undo a commit in github
How to undo a commit in GitHub - SheCanCode
May 27, 2025 - If not, use the revert command, that way the history of undoing your commit is preserved. The command below also works if you want to undo the last commit you have locally: A great hack is to add a number to the end of `~` to undo multiple commits.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-revert-multiple-git-commits
How to Revert Multiple Git Commits? - GeeksforGeeks
June 5, 2025 - However, Git does not provide a direct way to revert a range of commits with one command. ... This method is efficient for reverting multiple commits in a sequence without manually specifying each commit.
🌐
Squash
squash.io › how-to-revert-multiple-git-commits
How to Revert Multiple Git Commits - Squash Labs
October 28, 2023 - In this guide, we will explore two methods for reverting multiple Git commits: using the "git revert" command and using the "git reset" command.
🌐
Git
git-scm.com › docs › git-revert
Git - git-revert Documentation
Note: git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset[1], particularly the --hard option.
🌐
Delft Stack
delftstack.com › home › howto › git › git revert multiple commits
How to Revert Multiple Commits in Git | Delft Stack
March 11, 2025 - Reverting is a safe way to backtrack while preserving your project’s history, making it easier to collaborate with others. One of the most straightforward ways to revert multiple commits is by using the git revert command.
🌐
GoLinuxCloud
golinuxcloud.com › home › devops › git revert command explained (9 practical examples)
Git Revert Command Explained (9 Practical Examples) | GoLinuxCloud
March 16, 2026 - Each revert commit reverses the changes introduced in the original commits while preserving the complete commit history. Merge commits behave differently from normal commits because they contain changes from multiple parent branches.
🌐
Medium
sagrawal003.medium.com › git-revert-multiple-commits-at-a-time-technolize-your-future-49f78c2f0217
Git Revert multiple commits at a time — Technolize Your Future | by Sandeep Agrawal | Medium
October 23, 2020 - // Below is based on the commits ... --no-commit commit-id-3 git revert --no-commit commit-id-2 git commit -m "the commit message"// and then push your code....
🌐
Daniel Genezini
blog.genezini.com › p › git-revert-multiple-commits-of-protected-branches-with-git-restore
Git Revert Multiple Commits of Protected Branches with Git Restore
June 10, 2025 - A typical approach involves using git revert to create a commit that reverses changes from a range of commits:
🌐
Serebrov
serebrov.github.io › html › 2014-01-04-git-revert-multiple-recent-comments.html
git - how to revert multiple recent commits • vim, git, aws and other three-letter words
January 4, 2014 - We need to revert a range of revisions from B1 to B3. The range specified with two dots like <rev1>..<rev2> includes only commits reachable from <rev2>, but not reachable from <rev1> (see man -7 gitrevisions). Since we need to include B1 (represented by HEAD~2) we use HEAD~2^ (its parent) or HEAD~3 (also parent of HEAD~2). The HEAD~2^ syntax is more convenient if commit SHAs are used to name commits.
🌐
Medium
medium.com › @jessicatemporal › undoing-more-than-one-commit-at-once-with-git-revert-65ac4d5d0255
Undoing more than one commit at once with git revert | by Jessica Temporal | Jun, 2026 | Medium
1 week ago - # Include the oldest commit too: git revert -n <oldest>^..<newest> # Or exclude it (if that’s what you want): git revert -n <oldest>..<newest> If you get conflicts, resolve them, then stage the files: ... Originally published at ...
🌐
Dead Simple Chat
deadsimplechat.com › blog › git-revert-how-to-the-complete-guide
Git Revert: How to the complete guide
January 4, 2025 - In Git, the commits are in a chronological chain. When you revert a commit that is far back in the commit chain, you are effectively undoing changes that might have built upon by multiple subsequent commits.