Git Revert



The git revert command helps undo an existing commit. However, it's a special undo operation. It's does not remove any commit from the commit history. Instead, it will create a new commit with the opposite effect of an existing one.

The git revert command prevents losing commit history, vital for the integrity of the commit history, and appropriate collaboration.

The git revert should be used when there's a need to apply the opposite of a commit from the project history. It's mostly used for fixing bugs.


How Revert works

The git revert command takes a specified commit. However, it does not move the reference pointers to commit, unlike other undo commands as git rest and git checkout.

The revert operation takes the specified commit, figures out how to inverse the change of it, and create a new revert commit.

To explain lets considerate the following example:

$ mkdir git_revert_example
$ cd git_revert_example
$ git init 
Initialized empty Git repository in /git_revert_example/.git/
$ echo "Initial content" >> demo_file.txt
$ git add demo_file.txt
$ git commit -m "Initial commit"
[master (root-commit) eed1cef] Initial commit
    1 file changed, 1 insertion(+)
    create mode 100644 demo_file.txt
$ echo "wrong content" > demo_file.txt
$ git commit -am "wrong content"
    [master 128be37] wrong content
    1 file changed, 1 insertion(+)
$ git log --oneline 
128be37 (HEAD -> master) wrong content
eed1cef Initial commit

In the above example, a new git repository is initialized. There are two commits in the commit history, and the last one is a mistake. We set up our example, so we can use git revert:

$ git revert HEAD
[master 92b836f] Revert "wrong content"
 1 file changed, 1 deletion(-)

Git revert needs a reference to a commit, so here above, we passed in the HEAD reference to revert the last commit. A revert creates a new commit by opening the configured system editor to change the commit message. Now we can run a git log to check the new revert commit which has been added.

$ git log --oneline
92b836f (HEAD -> master) Revert "wrong content"
128be37 wrong content
eed1cef Initial commit

The above output shows the new commit that undoes the change of the last commit. Instead of removing the wrong commit, git revert append a new commit to undoing the specified commit.


Common options

Git revert has several options. The following list shows different use cases :

  • -e or --edit: It's the default option, and it is not required. It used to edit the commit message before reverting.

    $ git revert -e <commit_reference_value>
    
  • --no-edit: It will not open the text editor. It is the opposite of the -e option.

    $ git revert --no-edit <commit_reference_value>
    
  • -n or -no-commit: It will undo the changes from the specified commit and include them in the working directory and staging index. It will prevent from creating a new commit to undo changes.

    $ git revert -n <commit_reference_value>
    

Reverting vs. Resetting

The git revert revokes one specified commit while the git reset reverts back to the previous project state by removing succeeding commits.

Reverting has two major advantages over resetting:

  1. Reverting is a safe operation for the commits that have been already published to a remote repository. Reverting does not change the commit history, so no harm can be done using git revert.
  2. Reverting can select a specific commit at any point in history. On the other side, git reset can only operate backward from the current commit.


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.