开发者

How do you delete a commit in Mercurial?

I want to comp开发者_JS百科letely delete a Mercurial commit as if it was never entered in the repository and move back to my prior commit.

Is this possible?


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.


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


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


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.


If it's more than one commit and/or you already pushed it somewhere else, you can clone your repository and specify the last changeset that should be cloned.

See my answer here how to do this:
Mercurial: Fix a borked history

If you only committed locally and didn't push, you can just create a clone locally (as described in my link) and you're done.

If you already pushed to some remote repository, you would have to replace that with your clone.
Of course it depends if you are able (or allowed) to do this.


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


If using tortoise you can use modify history > strip...


Yes. Unless I am mistaken, as of v2.3 (rel. 2012/08/01) you can use the HisteditExtension with a drop command to drop a commit, along with strip or backout to remove changes.

A simple Google search on the feature: https://www.google.com/webhp#q=histedit+drop


In 2022 I do use evolve extension. It is one of the best extensions for this purpose.

To prune unwanted changeset, if you for example did a quick hack to get the code working:

$ echo 'debug hack' >> file1.c
$ hg commit -m 'debug hack'

Now you have a proper patch you can do hg prune .:

$ hg prune .
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
working directory is now at 2a39221aaebb
1 changesets pruned

If you push the change to the remote repository you will find only obsolescence markers:

$ hg push
searching for changes                          
no changes found                               
remote: 1 new obsolescence markers             

To check the changes to your local repo you can pull from the remote one:

$ hg pull                            
pulling from ssh://userid@server/repo
searching for changes      
no changes found
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜