Delete last commit in bitbucket

catherine picture catherine · Feb 12, 2013 · Viewed 256.8k times · Source

I made a mistake and I don't know how to delete my latest push in the repository. I pull the latest updates of the app but it has conflicts and I push it to repository.

How to delete my last commit? Or how to fix it?

Answer

asermax picture asermax · Feb 13, 2013

In the first place, if you are working with other people on the same code repository, you should not delete a commit since when you force the update on the repository it will leave the local repositories of your coworkers in an illegal state (e.g. if they made commits after the one you deleted, those commits will be invalid since they were based on a now non-existent commit).

Said that, what you can do is revert the commit. This procedure is done differently (different commands) depending on the CVS you're using:

On git:

git revert <commit>

On mercurial:

hg backout <REV>

EDIT: The revert operation creates a new commit that does the opposite than the reverted commit (e.g. if the original commit added a line, the revert commit deletes that line), effectively removing the changes of the undesired commit without rewriting the repository history.