Rollback Git Commit

There are two ways to rollback commits. One is to get the change at a certain previous commit and overwrite the current file and commit it. The other is remove all the histories up to the previous commit you want to rollback to.

The former is the safer method and the latter is quite dangerous. I will write how to do the former first.

git log

Let’s take a look at the history by executing the following command in your terminal under the directory where you have your git database.

$ git log

You will see an output like the following.

commit 13bf05be4456b90ebb8e9b358ff3c0b7dd798181 (HEAD -> master)
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:50:24 2019 -0700

    change 3

commit 49b50e681c54ccb65cfaf23840e8e7fa7cac0799
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:49:38 2019 -0700

    change 2

commit 04c32da81c03ef8c74d8cad76b5335b6a8f81bdb
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:49:08 2019 -0700

    change 1

commit 239ae3ea1b395d89bb2790566a16725e564da01b
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:48:41 2019 -0700

    initial commit

The log shows that I have committed 4 times in this repo with the hash for each commit. I’m going to try to revert back to the change 1.

git revert

We will enter git revert [hash] in the terminal.

$ git revert 239ae3ea1

You can just use the first 5 to 6 characters for the hash as long as Git can recognize it as unique. After you execute the command above you will see, an output like the following.

error: could not revert 04c32da... change 1
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

When you take a look at the target file, you will see something like the text below.

$ cat test.txt 
<<<<<<< HEAD
change 3
=======
>>>>>>> parent of 04c32da... change 1

At this point, you have to resolve the conflict by opening the file up with a text editor. We will remove all the text but “change 1” in the text file. After that, stage it and then commit it.

$ git add test.txt
$ git commit -m 'reverting back to change 1'

Once you commit it, the revert commit is at the top of the existing histories. This means the previous commits are preserved.

$ git log
commit 4ca6c0b07678072b3097ed34b8f085271eeaa274 (HEAD -> master)
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 23:09:17 2019 -0700

    reverting back to change 1

commit 13bf05be4456b90ebb8e9b358ff3c0b7dd798181
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:50:24 2019 -0700

    change 3

commit 49b50e681c54ccb65cfaf23840e8e7fa7cac0799
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:49:38 2019 -0700

    change 2

commit 04c32da81c03ef8c74d8cad76b5335b6a8f81bdb
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:49:08 2019 -0700

    change 1

commit 239ae3ea1b395d89bb2790566a16725e564da01b
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:48:41 2019 -0700

    initial commit

git reset

I’m going to write about the dangerous way. This command removes all the history newer than the point you want to go back to. Executing git log will gives us an output like the following.

$ git log
commit 13bf05be4456b90ebb8e9b358ff3c0b7dd798181 (HEAD -> master)
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:50:24 2019 -0700

    change 3

commit 49b50e681c54ccb65cfaf23840e8e7fa7cac0799
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:49:38 2019 -0700

    change 2

commit 04c32da81c03ef8c74d8cad76b5335b6a8f81bdb
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:49:08 2019 -0700

    change 1

commit 239ae3ea1b395d89bb2790566a16725e564da01b
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:48:41 2019 -0700

    initial commit

Let’s say I want to go back to “change 1”. I’m going to enter the following command to rollback to it and remove all the newer histories.

$ git reset --hard 04c32da81

Once the command is executed, if you run git log, you will see an output like this.

$ git log
commit 04c32da81c03ef8c74d8cad76b5335b6a8f81bdb (HEAD -> master)
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:49:08 2019 -0700

    change 1

commit 239ae3ea1b395d89bb2790566a16725e564da01b
Author: Hayato Iriumi <hiriumi@gmail.com>
Date:   Sun Sep 8 22:48:41 2019 -0700

    initial commit

Again, this can be really dangerous, so I would not use git reset --hard for most cases.

Recap

Make sure to use git revert to preserve the newer commits. Try not to use git reset --hard unless it’s really necessary.

Author: admin

A software engineer in greater Seattle area

Leave a Reply

Your email address will not be published. Required fields are marked *