How to use Git Revert

git revert makes a new commit

git revert simply creates a new commit that is the opposite of an existing commit.

It leaves the files in the same state as if the commit that has been reverted never existed. For example, consider the following simple example:

$ cd /tmp/example
$ git init
Initialized empty Git repository in /tmp/example/.git/
$ echo "Initial text" >
$ git add
$ git commit -m "initial commit"
[master (root-commit) 3f7522e] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644
$ echo "bad update" > 
$ git commit -am "bad update"
[master a1b9870] bad update
 1 file changed, 1 insertion(+), 1 deletion(-)

In this example the commit history has two commits and the last one is a mistake. Using git revert:

$ git revert HEAD
[master 1db4eeb] Revert "bad update"
 1 file changed, 1 insertion(+), 1 deletion(-)

There will be 3 commits in the log:

$ git log --oneline
1db4eeb Revert "bad update"
a1b9870 bad update
3f7522e initial commit

So there is a consistent history of what has happened, yet the files are as if the bad update never occured:

Initial text

It doesn’t matter where in the history the commit to be reverted is (in the above example, the last commit is reverted – any commit can be reverted).

Closing questions

do you have to do something else after?

A git revert is just another commit, so e.g. push to the remote so that other users can pull/fetch/merge the changes and you’re done.

Do you have to commit the changes revert made or does revert directly commit to the repo?

git revert is a commit – there are no extra steps assuming reverting a single commit is what you wanted to do.

Obviously you’ll need to push again and probably announce to the team.

Indeed – if the remote is in an unstable state – communicating to the rest of the team that they need to pull to get the fix (the reverting commit) would be the right thing to do :).

Leave a Comment