Revert or undo last git commit

If you have missed out my previous posts on git repositories then you can read it here.

While working on GIT distributed version control system , you might have found yourself in a scenario where you have to revert or undo last git commit. Reason for revert can be anything like wrong file checked in , some junk code has been checked in that can impact your well developed application etc.

Now here two scenario exists depends on the level of your commit–
1. If the commit has been already pushed to central remote repository that means now it is a public commit that means other developers can see these changes.
2. Commit is only made in your local cloned repository and push is pending that means it is a local commit that means nobody can see these changes except you.
Here , we will see the all the possible ways in which we can revert or undo a local commit and a public commit.
For better demonstration we will see it with the help of an example :-

I have cloned a remote repository into my local system and I made a local commit that I was not supposed to make. So, when I did git status it showed me the file that I edited by mistake and committed it in image 1 and image 2 respectively. And after executing git log you can see the that our previous commit is successfully registered(image 3).
Revert or undo last git commit
Revert or undo last git commit
Now after doing commit I realized that there is a problem with my commit and I have to revert it immediately. For undoing or revert the commit ,git provides multiple solutions.

Solution 1 : Change all the files with the opposite changes that you made in previous commit and make one more new commit . This is most straightforward and conventional way of doing this , if you are a git beginner and not very familiar with the git commands then this is for you.

Solution 2 : You can execute the git revert command by passing the commit id as an arguments and git will undo or revert your commit identified by the id provided.

git revert 5cf76aa793c886084cccfce763a4ed0811b482af

Undo or revert a last commitSo, now when you execute the above mentioned command it will revert your last commit changes and will make one more commit that will overwrite the changes made by you in the last commit and will bring the head to the previous position. You can also see , what happened internally by executing git log command.

git log

Revert or undo last git commitAfter that just do a normal push to include these changes in the repository and you will see that your changes has been successfully reverted without any manual intervention.

git push origin

Note : Above mentioned revert command works in the case of both local and public commit.

There are other solution also available to achieve the same thing , but i found git revert as the most convenient and easy to use.

Let'sConnect

Saurabh Jain

A Developer working on Enterprise applications ,Distributed Systems, Hadoop and BigData.This blog is about my experience working mostly on Java technologies ,NoSQL ,git , maven and Hadoop ecosystem.
Let'sConnect

One thought on “Revert or undo last git commit

Add Comment Register



Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>