Hg: delete recent commits

I have used Git in the past and I am Hg noob:

I have a repository structure:

o [> default] commit A | o commit B . . . o <a-tag] | 

I updated the commit with a-tag and made several other commits. Now i have

 o [> default] commit C | o commit D | | o [default] commit A | | | o commit B | . | . | . | / o <a-tag] | 

Now (before pushing) I understand that my commit C and commit D commits are based on incorrect commit. How can I return to its original state (without re-cloning the repository) by discarding these commit C and commit D commits (binding to git reset --hard a-tag )?

+7
source share
4 answers

You can use strip to permanently delete commits and all its descendants. In your case, you need to specify the version identifier "D":

 hg strip -r D 

Note: The mq extension must be enabled:

 [extensions] mq= 

Mercury backup packages with shared change sets in .hg / strip-backup, so this operation is pretty safe.

+8
source

You speak without cloning, and I get to that, but first let me point out that this is:

 cd .. hg clone -r -2 yourrepo yournewrepo 

is an instant action that gives you a new clone, locally, without the last two commits in your old repo. Since it is a local clone that uses hard links (even on Windows), so the repository does not take up additional disk space.

This is a classic Mercurial solution. Mercurial was built with the idea of ​​an unchanging story. If there is something in the story, you regret doing the opposite (which is what the backout does), so the story shows the error and its correction - like the journal of the journal of scientists. If you cannot / could not remain in history, you would make a clone that excludes it, as I showed above.

Not everyone can be so ... hardcore ... about their story, so many extensions have appeared that will change the story, but they must be specifically included. In your case, the rebase extension that you already installed (it now ships with Mercurial) will do exactly what you want. You just need to include it in your ~/.hgrc and then repeat D on A. If you really want C to leave, the strip command from the mq extension will do this. It also comes with Mercurial.

+4
source

I would just clone from the appropriate point and delete the old repo. Without cloning, consider hg backout to cancel the first C and then D, or if this is your final hg rebase to move C and D to commit A

+1
source

Backout C + backout D will remove these 2 commits, but add an extra 3 (backout + backout + merge). "Clear" deletion from history may be

If you want the wrong parent to change to fix , you need to use rebase, as @chill mentioned

+1
source

All Articles