Git Revert
In short
Git revert is a Git command that undoes an earlier commit by creating a new commit with the opposite changes, leaving the existing project history intact.
What is git revert?
git revert is the safe way to undo a commit that has already been shared. Instead of deleting the commit, Git works out the exact opposite of its changes (lines it added are removed, and lines it removed come back) and records that as a brand-new commit. The original commit stays in the history, followed later by a commit whose message begins with 'Revert'.
You pass the hash of the commit to undo, as in git revert a1b2c3d, and Git opens an editor for the message, or uses the default one if you add --no-edit. Because nothing already published is rewritten, teammates simply pull the revert like any other commit, which makes it the standard way to back out a bad change on main or a release branch. Reverting a merge commit needs -m 1 to tell Git which parent is the main line to keep.
Reverting is like printing a correction in a newspaper rather than recalling every copy already delivered: the original article stays on record, and the correction explains what changed. If later commits also touched the lines being reverted, Git may report a conflict, which you resolve as usual and finish with git revert --continue.
The most common confusion is git revert versus git reset. Reset moves a branch back and makes later commits disappear from it, which rewrites history and breaks other people's copies if those commits were pushed, while revert only ever adds a new commit. Revert is also narrower than a deployment rollback: a rollback puts a previous release back into production, while a revert changes the code itself, and teams often do both.
Key takeaways
git revert <hash>creates a new commit that reverses the changes of an earlier one.- History is not rewritten, so it is safe to use on shared branches like
main. - Reverting a merge commit requires
-m 1to choose which parent to keep. - Conflicts are resolved as usual and finished with
git revert --continue. - Use
git resetfor unpushed local commits andgit revertfor published ones.
Example
# Find the commit that introduced the problem
git log --oneline
# 9f8e7d6 Add discount banner
# a1b2c3d Change checkout validation <- this one broke checkout
# Create a new commit that undoes it, using the default message
git revert --no-edit a1b2c3d
# Undo a merge commit, keeping the first parent (usually main)
git revert -m 1 4d5e6f7
# Share the fix like any other commit
git pushReaders ask
What is the difference between git revert and git reset?
git revert undoes a commit by adding a new commit with the opposite changes, so history only grows. git reset moves a branch back to an earlier commit and drops the later ones, which rewrites history and should be limited to commits you haven't pushed.
Can I revert several commits at once?
Yes. Pass several hashes, or a range such as git revert A..B for every commit after A up to and including B, and Git creates one revert commit for each. Add --no-commit to stage all the reversals and record them as a single commit instead.
How do I undo a revert?
Revert the revert commit itself with git revert <revert-hash>, which brings the original changes back as another new commit. This is the usual way to re-apply a change once the underlying problem has been fixed.
Often compared
See also
- Git ResetVersion Control, p. 20Git reset is a Git command that moves the current branch back to an earlier commit and, depending on its mode, also resets the staging area and your files.
- CommitVersion Control, p. 5A commit is a saved snapshot of a project's files in Git, recorded with a unique ID, an author, a timestamp, and a message describing what changed.
- MergeVersion Control, p. 29A merge in Git combines the changes from one branch into another, joining separate lines of development back together into a single, shared history.
- Cherry-pickVersion Control, p. 3A cherry-pick in Git copies the changes from one specific commit onto your current branch as a new commit, without merging the rest of the branch it came from.
- RollbackDevOps & Cloud, p. 45A rollback is the process of returning software to a previous, known-good version after a new deployment causes errors, outages, or other unexpected problems.
- Git PushVersion Control, p. 18Git push is a Git command that uploads your local commits to a remote repository, updating the matching branch there so others can see and use your work.
Spotted a mistake or something missing on this page?Suggest an edit