I’m working with a Git repository and I need to undo several commits while keeping the history intact. My branch currently looks like this:
A <- B <- C <- D <- HEAD
I want to reset the branch so that HEAD points back to commit A, essentially undoing B, C, and D. Since some of these commits have already been pushed, I can’t simply rebase or reset, I need to use git revert instead.
I’m wondering how to properly git revert multiple commits. Do I need to revert each commit individually, and if so, in what order should I do it? Is there a way to revert a range of commits in one command?
Any advice, best practices, or examples for safely reverting multiple commits would be greatly appreciated.
The most straightforward way is to revert commits one by one, starting from the most recent down to the oldest you want to undo. In your example:
A <- B <- C <- D <- HEAD
You’d run:
git revert D
git revert C
git revert B
Reverting from newest to oldest helps avoid conflicts and ensures that each commit is undone cleanly. I usually do this when the commits are already pushed to a shared branch, as it preserves history safely.
Git also allows you to revert a range of commits with the … syntax. For example, to revert B through D in one go:
git revert B..D
Be careful: git revert B…D reverts commits after B up to D, so double-check the range to make sure you’re including all the commits you want. I like this method when I want fewer revert commits cluttering the history.
If you’re unsure about the exact commits or want to preview changes before applying, tools like GitKraken, SourceTree, or even git log combined with git revert -n (no commit) can help. For instance:
git revert -n B C D
This stages all the reverts without committing, letting you review the combined changes before committing them in a single commit. It’s a lifesaver for avoiding messy histories or accidental conflicts.