How To Revert/Undo The Last Commit in Git

Sarath Pillai's picture
Undo The Last Git Commit

Git is the most popular version controlling tool out there. It is super powerful and provides a lot of features. But these features might appear confusing in the beginning. The very reason for using a version controlling tool is to track the changes and revert or go back to a previous state.  

One common issue that I have heard people discuss and do often is to revert or undo the very last commit you did (and also pushed to the remote repository sometimes).

Lets imagine that i have a git repository cloned at /tmp/clone1. It has three commits in total. Basically three files and three commits for each of those files.

 

root@localhost:/tmp/clone1# ls
a  b  c
root@localhost:/tmp/clone1# git log
commit 472581378663a1c118e6256f0dfb4ed3794af769
Author: Sarath Pillai <sarath@slashroot.in>
Date:   Wed Aug 2 16:10:33 2017 +0000

    Adding c

commit 2007396300fb76faa514338aa1cdc94307d8a4fa
Author: Sarath Pillai <sarath@slashroot.in>
Date:   Wed Aug 2 16:09:43 2017 +0000

    Adding b

commit d935154a3a7198c4efe198ec0aabd38c5b015fc2
Author: Sarath Pillai <sarath@slashroot.in>
Date:   Wed Aug 2 16:09:32 2017 +0000

    Adding a

 

See the the git log output above, the repository has three commits and three files(a, b and c). I am now going to add one more file to it and then do a git push.

 

First Method to Undo The Last Commit:

 

root@localhost:/tmp/clone1# touch d
root@localhost:/tmp/clone1# git add d
root@localhost:/tmp/clone1# git commit -m "Adding d"
[master b69aac5] Adding d
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 d
root@localhost:/tmp/clone1# ls
a  b  c  d
root@ci:/tmp/clone1# git push

 

Ooops. I have pushed it to the remote repository as well. But i actually never wanted that file to be pushed or committed. So i now want to revert that commit from my remote as well as local. Lets do that.

Make a note of the last commit of file d we did above. Especially note the line  after you hit commit ([master b69aac5]). It is the commit id short form. You can also grab this from git log.

We will first fix the remote where we pushed. So we will go ahead and ask our remote to delete the very last commit(we will give the commit short form). See below.

 

root@localhost:/tmp/clone1# git push origin +b69aac5^:master
Total 0 (delta 0), reused 0 (delta 0)
To file:///opt/git-repo/.git
 + b69aac5...4725813 b69aac5^ -> master (forced update)

 

 

The above git push command will remove the commit b69aa5 from your remote master branch.

Once this is done, we also need to remove it from our local repository. This can be done as shown below.

 

root@localhost:/tmp/clone1# git reset HEAD^ --hard
HEAD is now at 4725813 Adding c

 

 

Now we have totally removed that last git commit from the remote location as well as our local repo. 

 

Second Method to Undo The Last Commit:

Another easier method if you do not like the previous one (which ofcourse involve noting down the short commit id), is to simply execute the below two commands.

 

root@localhost:/tmp/clone1# git reset HEAD^ --hard

 

 

After the above command, your last commit is removed from the local repository. Now forcefully push the local repository to the remote with the below command.

 

root@localhost:/tmp/clone1# git push origin -f

 

Third Method to Undo The Last Commit:

 

Both the above methods works well. But is not always recommended. If you noticed, those two methods will alter and modify the git log history as well. So a recommended way of deleting the very last commit is by using git revert command as shown below.

Once again lets create that "d" file and push it after a commit.

 

root@localhost:/tmp/clone1# touch d
root@localhost:/tmp/clone1# git add d
root@localhost:/tmp/clone1# git commit -m "Adding d"
[master 5e267c4] Adding d
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 d
root@localhost:/tmp/clone1# git push

 

 

So our last commit ID now is 5e267c4 (see above). Using git revert lets go ahead and remove that last commit.

 

root@ci:/tmp/clone1# git revert 5e267c4
[master 66272c7] Revert "Adding d"
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 d

 

Please note the fact that the above command will open a commit message editor, and you can edit the message. By default it will say something like Revert "Adding d" (which is the commit message of the commit that we are reverting).

Now simply do a git push, so that the remote also removes your last commit.

 

root@ci:/tmp/clone1# git push

 

So which one to use out of these three?. Well i will recommend the last one. Because it does not play with git history and log. However, if you are removing some important sensitive data from the last commit (say for example you commited some credentials and passwords), then i would chose the first two.

Rate this article: 
No votes yet

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.