How to Revert a Commit Already Pushed to a Remote Repository

Posted By : Kamaldeep Singh | 31-Dec-2015
How to Revert a Commit Already Pushed to a Remote Repository

 

In this blog I would like to tell you how to revert your last commit from git hub. There may be a case when you realize that one of the commits should not be there, or that there was some unacceptable typo in it. So that isn't a big problem. All you need to take care of is that you should do it fast before anyone fetches the bad commits.

 

This can be done in two ways :-

  • Correct the mistake in a new commit
  • Revert the full commit

 

Correct the mistake in a new commit

This is simple as you just need to remove or fix the bad file in a new commit and push it to the remote repository. This is the most natural way to fix an error, always safe and totally non-destructive, and how you should do it 99% of the time. Yet the bad commit remains there and accessible, but this is usually not a big deal.

 

Revert the full commit

In this case, instead of going through all the changes manually your whole previous commit will be reverted. Reverting a commit means to create a new commit that undoes all changes that were made in the bad commit. Just like above, the bad commit remains there, but it no longer affects the the current master and any future commits on top of it. To do this, first reset the branch to the parent of the current commit, then force-push it to the remote as follows.

                $git reset HEAD^ --hard
                $git push -f
            

 

For more details, Click Here

THANKS

About Author

Author Image
Kamaldeep Singh

Kamaldeep is a highly skilled Backend Developer specializing in Java, specifically the Spring framework. He also has extensive knowledge of Javascript and associated frameworks such as Node.js and Express. He possesses a deep understanding of the latest technologies and has hands-on experience with Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as both relational databases like MySQL and PostgreSQL and non-relational databases like MongoDB. He has made significant contributions to various projects, including Viral Nation, ExamWorks, TNIBRO, Biogas engineering, SecureNow - Web Application, FB Messenger Chatbot, Dialogflow Chatbot, and Catalyst. Kamaldeep's expertise allows him to seamlessly integrate different technologies into applications, highlighting his adaptability and innovative mindset. His practical experience and strong technical skills make him an invaluable asset to any team.

Request for Proposal

Name is required

Comment is required

Sending message..