git revert
On this page, you will learn about the git revert command, find out the difference between git revert and git reset, see the options and usage examples.

Description
The git revert command is an “undo” operation. Unlike other undo commands, it does not rewrite history. The git revert command reverts the changes introduced by the commit and appends a new commit with resulting reversed content. This does not allow Git to lose history, which is essential for revision history integrity and proper collaboration. Reverting is used for applying the inverse commit from the project history. You can use git revert for automatically going back and making fixes.
Options
-e --edit | Opens up the configured system editor and prompts you to edit the commit message before committing the revert. Default option. |
|---|---|
--no-edit | Causes the revert not to open the editor (the opposite of the -e option). |
-n --no-commit | Adds the inverse modifications to the Staging Index and Working Directory instead of creating a new commit. |
-m <parent-number> | For reverting merge commits, specifies the parent commit to revert against. |
Examples
# Stage the reverted changes without committing them (useful for review)
git revert -n HEAD
# Revert a merge commit against its first parent
git revert -m 1 <merge-commit-hash>How it works
Like git checkout and git reset, git revert also takes a specified commit but it does not move reference pointers to this commit. The revert operation takes the specified commit, inverses the modifications from that commit and creates a new revert commit.
Here is an example of creating a repository:
git revert command
mkdir git_revert_example
cd git_revert_example/
git init .
#Initialized empty Git repository in /git_revert_example/.git/
touch w3docs_file
git add w3docs_file
git commit -m "original commit"
#[master (root-commit) 299b15f] original commit
#1 file changed, 0 insertions(+), 0 deletions(-)
#create mode 100644 w3docs_file
echo "original content" >> w3docs_file
git commit -m "add new content to w3docs_file"
#[master 3602d88] add new content to w3docs_file
#1 file changed, 1 insertion(+)
echo "prepended line content" >> w3docs_file
git commit -m "prepend content to w3docs file"
#[master 86bb32e] prepend content to w3docs file
#1 file changed, 1 insertion(+)
git log --oneline
#86bb32e prepend content to w3docs file
#3602d88 add new content to w3docs file
#299b15f original commitIn this example, a repository is initialized in a newly created directory named git_revert_example. There are 3 commits to the repository in which a file named w3docs_file is added. Its content has been changed twice. We use git log at the end of the repository set up to show all 3 commits in the commit history. Now we can invoke git revert:
git revert
git revert HEAD
#[master b9cd081] Revert "prepend content to w3docs file"
#1 file changed, 1 deletion(-)git revert will not work without passing a commit reference. In the given example, it has been passed in the HEAD reference to revert the last commit. A revert creates a new commit which opens up the configured system editor creating a new commit message. We can use git log and see the new commit added to the previous log:
git log --oneline
git log --oneline
#2365e21 Revert "prepend content to w3docs_file"
#23ab21e prepend content to w3docs file
#3602d88 add new content to w3docs_file
#345b23f original commitAfter the revert the 3rd commit is still in the project history. The git revert added a new commit to undo its changes instead of deleting.
Resetting vs. reverting
The git revert command undoes only one commit, while git reset reverts back to a previous project state by deleting succeeding commits.


Reverting is considered a safe operation for commits that have been published to a shared repository. Another advantage of reverting is targeting a specific commit at a random point in the history. Avoid using git revert on private or unpublished commits where git reset is more efficient. Find detailed information about git reset on our next page.
Practice
What are the characteristics and options of the 'git revert' command?