Revert – undo the changes introduced by a commit

Revert can be used to undo a commit in history that has already been published (pushed), whereas this can't be done with the amend or reset options without rewriting history.

Revert works by applying the anti-patch introduced by the commit in question. A revert will, by default, create a new commit in history with a commit message that describes which commit has been reverted.

Getting ready

Again, we'll use the hello world repository. Make a fresh clone of the repository, or reset the master branch if you have already cloned.

We can create a fresh clone as follows:

$ git clone https://github.com/dvaske/hello_world_cookbook.git
$ cd hello_world_cookbook

We can reset the existing clone as follows:

$ cd hello_world_cookbook
$ git checkout master
$ git reset --hard origin master
HEAD is now at 3061dc6 Adds Java version of 'hello world'

How to do it...

  1. First, we'll list the commits in the repository:
    $ git log --oneline
    3061dc6 Adds Java version of 'hello world'
    9c7532f Fixes compiler warnings
    5b5d692 Initial commit, K&R hello world 
    
  2. We'll revert the second commit, b8c39bb:
    $ git revert 9c7532f
    Revert "Fixes compiler wanings"
    
    This reverts commit 9c7532f5e788b8805ffd419fcf2a071c78493b23.
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # On branch master
    # Your branch is up-to-date with 'origin/master'.
    #
    # Changes to be committed:
    #       modified:   hello_world.c
    #
    ~
    ~
    ~
    "~/aske/packt/repos/hello_world_cookbook/.git/COMMIT_EDITMSG" 12L, 359C
    [master 9b94515] Revert "Fixes compiler warnings"
     1 file changed, 1 insertion(+), 5 deletions(-)
    
  3. When we check the log, we can see that a new commit has been made:
    $ git log --oneline
    9b94515 Revert "Fixes compiler warnings"
    3061dc6 Adds Java version of 'hello world'
    9c7532f Fixes compiler warnings
    5b5d692 Initial commit, K&R hello world
    

    We can take a closer look at the two commits with git show if we want a closer investigation of what happened.

How it works...

Git revert applies the anti-patch of the commit in question to the current HEAD pointer. It will generate a new commit with the "anti-patch" and a commit message that describes the reverted commit(s).

There's more...

It's possible to revert more than one commit in a single revert, for example, git revert master~6..master~2 will revert the commits from the sixth commit from the bottom in master to the third commit from the bottom in master (both included).

It is also possible not to create a commit while reverting; passing the -n option to git revert will apply the needed patched but only to the working tree and the staging area.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset