Posted by Kamaldeep Singh | Last Updated: 27-Jan-16
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