How to Revert a Commit in Git
The git revert command creates a new commit that undoes the changes introduced by a specified commit. Unlike git reset
, which rewrites the commit history, git revert preserves the full history and is the safe way to undo changes that have already been pushed to a shared repository.
This guide explains how to use git revert to undo one or more commits with practical examples.
Quick Reference
| Task | Command |
|---|---|
| Revert the last commit | git revert HEAD |
| Revert without opening editor | git revert --no-edit HEAD |
| Revert a specific commit | git revert COMMIT_HASH |
| Revert without committing | git revert --no-commit HEAD |
| Revert a range of commits | git revert --no-commit HEAD~3..HEAD |
| Revert a merge commit | git revert -m 1 MERGE_COMMIT_HASH |
| Abort a revert in progress | git revert --abort |
| Continue after resolving conflicts | git revert --continue |
Syntax
The general syntax for the git revert command is:
git revert [OPTIONS] COMMIT-
OPTIONS— Flags that modify the behavior of the command. -
COMMIT— The commit hash or reference to revert.
git revert vs git reset
Before diving into examples, it is important to understand the difference between git revert and git reset, as they serve different purposes:
-
git revert— Creates a new commit that undoes the changes from a previous commit. The original commit remains in the history. This is safe to use on branches that have been pushed to a remote repository. -
git reset— Moves theHEADpointer backward, effectively removing commits from the history. This rewrites the commit history and should not be used on shared branches without coordinating with your team.
As a general rule, use git revert for commits that have already been pushed, and git reset
for local commits that have not been shared.
Reverting the Last Commit
To revert the most recent commit, run git revert followed by HEAD:
git revert HEADGit will open your default text editor so you can edit the revert commit message. The default message looks like this:
Revert "Original commit message"
This reverts commit abc1234def5678...Save and close the editor to complete the revert. Git will create a new commit that reverses the changes from the last commit.
To verify the revert, use git log to see the new revert commit in the history:
git log --oneline -3a1b2c3d (HEAD -> main) Revert "Add new feature"
f4e5d6c Add new feature
b7a8c9d Update configuration
The original commit (f4e5d6c) remains in the history, and the new revert commit (a1b2c3d) undoes its changes.
Reverting a Specific Commit
You do not have to revert the most recent commit. You can revert any commit in the history by specifying its commit hash.
First, find the commit hash using git log:
git log --onelinea1b2c3d (HEAD -> main) Update README
f4e5d6c Add login feature
b7a8c9d Fix navigation bug
e0f1a2b Add search functionality
To revert the “Fix navigation bug” commit, pass its hash to git revert:
git revert b7a8c9dGit will create a new commit that undoes only the changes introduced in commit b7a8c9d, leaving all other commits intact.
Reverting Without Opening an Editor
If you want to use the default revert commit message without opening an editor, use the --no-edit option:
git revert --no-edit HEAD[main d5e6f7a] Revert "Add new feature"
2 files changed, 0 insertions(+), 15 deletions(-)
This is useful when scripting or when you do not need a custom commit message.
Reverting Without Committing
By default, git revert automatically creates a new commit. If you want to stage the reverted changes without committing them, use the --no-commit (or -n) option:
git revert --no-commit HEADThe changes will be applied to the working directory and staging area, but no commit is created. You can then review the changes, make additional modifications, and commit manually:
git status
git commit -m "Revert feature and clean up related code"This is useful when you want to combine the revert with other changes in a single commit.
Reverting Multiple Commits
Reverting a Range of Commits
To revert a range of consecutive commits, specify the range using the .. notation:
git revert --no-commit HEAD~3..HEADThis reverts the last three commits. The --no-commit option stages all the reverted changes without creating individual revert commits, allowing you to commit them as a single revert:
git commit -m "Revert last three commits"Without --no-commit, Git will create a separate revert commit for each commit in the range.
Reverting Multiple Individual Commits
To revert multiple specific (non-consecutive) commits, list them one after another:
git revert --no-commit abc1234 def5678 ghi9012
git commit -m "Revert selected commits"Reverting a Merge Commit
Merge commits have two parent commits, so Git needs to know which parent to revert to. Use the -m option followed by the parent number (usually 1 for the branch you merged into):
git revert -m 1 MERGE_COMMIT_HASHIn the following example, we revert a merge commit and keep the main branch as the base:
git revert -m 1 a1b2c3d-
-m 1— Tells Git to use the first parent (the branch the merge was made into, typicallymainormaster) as the base. -
-m 2— Would use the second parent (the branch that was merged in).
You can check the parents of a merge commit using:
git log --oneline --graphHandling Conflicts During Revert
If the code has changed since the original commit, Git may encounter conflicts when applying the revert. When this happens, Git will pause the revert and show conflicting files:
CONFLICT (content): Merge conflict in src/app.js
error: could not revert abc1234... Add new feature
hint: After resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' and run 'git revert --continue'.
To resolve the conflict:
-
Open the conflicting files and resolve the merge conflicts manually.
-
Stage the resolved files:
Terminal git add src/app.js -
Continue the revert:
Terminal git revert --continue
If you decide not to proceed with the revert, you can abort it:
git revert --abortThis restores the repository to the state before you started the revert.
Pushing a Revert to a Remote Repository
Since git revert creates a new commit rather than rewriting history, you can safely push it to a shared repository using a regular push:
git push origin mainThere is no need for --force because the commit history is not rewritten.
Common Options
The git revert command accepts several options:
-
--no-edit— Use the default commit message without opening an editor. -
--no-commit(-n) — Apply the revert to the working directory and index without creating a commit. -
-m parent-number— Specify which parent to use when reverting a merge commit (usually1). -
--abort— Cancel the revert operation and return to the pre-revert state. -
--continue— Continue the revert after resolving conflicts. -
--skip— Skip the current commit and continue reverting the rest.
Troubleshooting
Revert is in progress
If you see a message that a revert is in progress, either continue with git revert --continue after resolving conflicts or cancel with git revert --abort.
Revert skips a commit
If Git reports that a commit was skipped because it was already applied, it means the changes are already present. You can proceed or use git revert --skip to continue.
FAQ
What is the difference between git revert and git reset?git revert creates a new commit that undoes changes while preserving the full commit history. git reset moves the HEAD pointer backward and can remove commits from the history. Use git revert for shared branches and git reset for local, unpushed changes.
Can I revert a commit that has already been pushed?
Yes. This is exactly what git revert is designed for. Since it creates a new commit rather than rewriting history, it is safe to push to shared repositories without disrupting other collaborators.
How do I revert a merge commit?
Use the -m option to specify the parent number. For example, git revert -m 1 MERGE_HASH reverts the merge and keeps the first parent (usually the main branch) as the base.
Can I undo a git revert?
Yes. Since a revert is just a regular commit, you can revert the revert commit itself: git revert REVERT_COMMIT_HASH. This effectively re-applies the original changes.
What happens if there are conflicts during a revert?
Git will pause the revert and mark the conflicting files. You need to resolve the conflicts manually, stage the files with git add, and then run git revert --continue to complete the operation.
Conclusion
The git revert command is the safest way to undo changes in a Git repository, especially for commits that have already been pushed. It creates a new commit that reverses the specified changes while keeping the full commit history intact.
If you have any questions, feel free to leave a comment below.
![]()
