How do you delete a commit in Mercurial?

I want to completely delete a Mercurial commit as if it was never entered in the repository and move back to my prior commit.

Is this possible?


Solution 1:

If it was your last commit and you haven't pushed it anywhere, you can do that with rollback. Otherwise, no. Not really. Time to change your passwords.

Edit: It has been pointed out that you can clone from an older revision and merge in the changes you want to keep. That's also true, unless you have pushed it to a repo you don't control. Once you push, your data is very likely to be very hard to get back.

Solution 2:

You can try to remove mq info about your commit.

  • For this you need to go File->Settings->Extensions.
  • There check mq and restart gui.
  • After that just right click on unneeded commit and ModifyHistory->Strip

Solution 3:

To edit the history I would use the Histedit Extension extension.

 hg histedit 45:c3a3a271d11c

However keep in mind this only makes sense in a situation where you have not yet pushed the commits to the public repository, you own the public repository and/or you can account for all the clones out there. If you receive the following error:

abort: can't rebase immutable changeset 43ab8134e7af

It means that Mecurial thinks this is a public changeset (see phases) that has already been pushed - you can force it to be a draft again doing:

hg phase -f -d 45:c3a3a271d11c

Solution 4:

I encounter this fairly often. I make a commit and then pull to push. But then there is something incoming that makes my newly made commit unnecessary. A plain hg rollback isn't enough because it only undoes the pull...

This is the thing to do:

hg strip <rev>

Things are painless when you don't push your changesets anywhere.

Solution 5:

You can use "hg backout" to do a reverse merge basically. All options are discussed in the freely available book "Mercurial: The Definitive Guide":

http://hgbook.red-bean.com/read/finding-and-fixing-mistakes.html