The git revert command is used to undo the changes
introduced by an earlier commit. Unlike git reset,
revert creates a new commit that reverses the effect
of the selected commit.
git revert creates a new commit that reverses the changes introduced by an earlier commit.
git revert <commit>
The original commit remains in the repository history.
Sometimes a commit has already been shared with other developers, but its changes need to be undone.
Instead of removing the existing commit from history, Git can create a new commit that reverses it.
The basic syntax is:
git revert <commit>
Replace <commit> with the commit identifier that
you want to reverse.
Use git log to find the commit you want to reverse.
git log --oneline
Example:
8f32abc Add contact page
5d21ef4 Add login page
1a72c90 Initial commit
Suppose you want to reverse commit 8f32abc:
git revert 8f32abc
Git prepares a new commit that reverses the changes introduced by that commit.
One of the most important features of git revert is that
it does not remove the original commit.
Commit A → Commit B → Commit C
↓
Revert Commit
The new revert commit becomes part of the project history.
| Command | Main Purpose | History |
|---|---|---|
git revert |
Undo a commit by creating a new commit | Preserved |
git reset |
Move branch reference and optionally change index/worktree | Can rewrite local history |
The choice depends on whether you want to preserve the existing commit history.
To revert the latest commit, you can use:
git revert HEAD
Git creates a new commit that reverses the changes introduced by the current HEAD commit.
You can also revert the parent of the current commit:
git revert HEAD~1
Here, HEAD~1 refers to the parent of the current HEAD.
Suppose the history is:
A → B → C
If you revert B, Git does not remove B. Instead, it creates a new commit that reverses B's changes:
A → B → C → Revert B
When Git creates a revert commit, it normally prepares a commit message describing the reversal.
You can edit the message before completing the commit.
git revert 8f32abc
The --no-edit option uses the automatically generated commit message without opening the editor.
git revert --no-edit 8f32abc
You can revert more than one commit by specifying a range when appropriate.
git revert HEAD~3..HEAD
This requests reversion of the commits reachable in the specified range. Review the resulting changes carefully before completing the operation.
A revert applies the inverse of a commit's changes to the current working tree and index and then creates a new commit when the operation completes successfully.
git revert 8f32abc
A revert can produce conflicts if the changes introduced by the selected commit no longer apply cleanly to the current files.
Git will stop and ask you to resolve the conflicts before completing the revert.
If a conflict occurs, use:
git status
Git will show files that require attention.
After opening the conflicted files, manually resolve the conflict. Then stage the resolved files:
git add filename
After all conflicts are resolved, continue the revert:
git revert --continue
If you decide that you do not want to continue a revert currently in progress, you can abort it:
git revert --abort
This attempts to return the repository to the state it had before the revert operation started.
Revert is useful when working with shared repositories because it adds a new commit instead of removing an existing commit from history.
git revert 8f32abc
git push
The new revert commit can then be shared with other developers.
If a commit has already been pushed to GitHub and needs to be undone, a revert can create a new commit that reverses the change.
git revert 8f32abc
git push
This keeps the original commit in the repository history.
Suppose a commit introduced a problem:
8f32abc Add new payment feature
You can reverse that commit:
git revert 8f32abc
The project can then contain a new commit that reverses the payment feature changes.
git log --oneline
git show 8f32abc
git revert 8f32abc
git log --oneline
This workflow lets you inspect a commit before deciding to reverse it.
A common misunderstanding is that revert deletes a commit. It does not.
Original Commit
↓
Revert Commit
Both commits remain part of the history.
After a successful revert, use:
git log --oneline
You can see both the original commit and the newly created revert commit.
These commands have different purposes:
git restore index.html
git revert 8f32abc
git log --oneline
git show 8f32abc
git revert 8f32abc
git revert HEAD
git revert --no-edit 8f32abc
git status
git revert --continue
git revert --abort
# View history
git log --oneline
# Inspect the commit
git show 8f32abc
# Revert the commit
git revert 8f32abc
# Check the result
git status
# View updated history
git log --oneline
This is a simple workflow for safely reviewing and reversing a commit.
git init
touch index.html
git add index.html
git commit -m "Create homepage"
touch about.html
git add about.html
git commit -m "Create about page"
git log --oneline
git revert HEAD
git log --oneline
git status
The last command sequence creates a new commit that reverses the latest commit while preserving the original commit in history.
The git revert command is used to undo the effect of an earlier commit by creating a new commit.
git revert <commit>
For the latest commit:
git revert HEAD
Unlike reset, revert keeps the original commit in history and adds a new commit that reverses its changes.
This makes revert particularly useful when changes have already been shared with other developers.
git revert HEAD reverts the current HEAD commit.git revert --no-edit uses the generated commit message.git status to inspect conflicts.git revert --continue after resolving conflicts.git revert --abort to cancel a revert in progress.Question: What does git revert do?