开发者

Abandoning changes without deleting from history

There is a commit that just didn't work, so I want to abandon it without deleting it from history.

I have updated from an earlier revision and committed, thus crea开发者_运维知识库ting a new head.


I don't have branches, I don't want branches, I just want to simply go on with the new head exactly as it is, nothing fancy, no merge, no worries, just go on forgetting the previous one.

I can't seem to find how to do that, and I'm starting to believe it can't be done. All I find is stuff about branches, or stuff about merging.


Update your repository to the head with the revision that you want to forget about, then use hg commit --close-branch to mark that (anonymous) branch as closed. Then update to the head of the branch that you do want, and continue working.

You can still see the closed branch if you use the -c option to hg heads, but it won't show up by default and hg merge will know not try to merge with the closed head.

You will need to use hg push --force the first time you push this closed head to another repository since you are actually create additional heads in the remote repository when you push. So tell Mercurial that this is okay with --force. People who pull the closed head wont be bothered by any warnings.


I know you don't want to work with branches at this stage, but that's exactly what you've done. When you went back to an earlier version and committed something that worked you created a branch - an unnamed branch, but a branch all the same.


There's no problem with just carrying on just as you are and not worrying about having multiple heads, but if you want to tidy things up so you don't accidentally pick the wrong head one time then you can kill off the old branch.

There's a good section in the Mercurial documentation that takes you through a number of options around Pruning Dead Branches.

I think the best option for you is to mark the old branch as "closed". If your old head is revision "123" then:

hg update -r 123
hg commit --close-branch -m 'Closing old branch'
hg update -C default


First of all, type:

hg heads

Imagine, you have three heads listed:

changeset:   223:d1c3deae6297
user:        Your name  <your@email.com>
date:        Mon Jun 09 02:24:23 2014 +0200
summary:     commit description #3

changeset:   123:91c5402959z3
user:        Your name <your@email.com>
date:        Sat Dec 23 16:05:38 2013 +0200
summary:     commit description #2

changeset:   59:81b9804156a8
user:        Your name <your@email.com>
date:        Sat Sep 14 13:14:40 2013 +0200
summary:     commit description #1

Let's say, you want to keep the last head active (223) and close the rest.

You would then do as follows:

Close head #59

hg up -r 59
hg ci --close-branch -m "clean up heads; approach abandoned"

Close head #123

hg up -r 123
hg ci --close-branch -m "clean up heads; approach abandoned"

Commit the changes

hg push

Don't forget to switch to the right head at the end

hg up -r 223

And you're done.


You want to use hg backout. This removes the changes made by the changeset from any child changeset.

Check this out for a good explanation. Mercurial Backout


An alternative to closing or stripping the unwanted branch would be to merge it in a way that totally discards its effects, but leaves it in history. This approach will allow those unwanted changes to propagate in a push - so only use this if that is the intended effect.

Let's say the changeset history looks like this:

1-2-3-4-5-6    
       \    
        7-8-*

and it is 5 and 6 which are no longer wanted.

You can do this:

hg up 8
hg merge -r 6 -t :local
hg commit ...

which will create this:

1-2-3-4-5-6    
       \   \
        7-8-9-*

The update to 8 ensures you are working at the desired head in history, which you want to keep.

The -t :local instructs hg to use the merge "tool" called local which tells it to ignore changes from the other branch, i.e., the one NOT represented by the current working folder state. More info.

Thus the unwanted changes in 5 and 6 are preserved in history but do not affect anything more recent.


Both Niall's and Nick's answers are straight on. Because I find myself creating lots of dangling heads, I ended up writing an alias to close heads more easily. By adding this to your .hgrc:

[alias]
behead = !REV=$($HG id -i); $HG update $@ -q && $HG ci --close-branch -m "Closing dead head" && $HG update $REV -q

(if you already have an [alias] section, you can append to it instead)

You can now close a head in one single-command (and without having to update to a different changeset manually) like this:

$ hg behead 123

Note: the alias takes advantage of the fact that Mercurial aliases can be shell commands. This means that this will probably only work on UNIX, not on Windows.


This is a use case for the Evolve extension. It's currently not bundled with Mercurial, so it is technically a third party extension. But it's being used quite heavily by a bunch of people, including Mercurial developers, is being very actively developed, and isn't going anywhere.

With the Evolve extension, you simply do

hg prune -r revname

and get on with your life. The cset will still be there, but obsoleted. It won't be visible unless you pass the --hidden option to Mercurial commands, and by default won't be pushed to remote repositories. Though I think you can force it if you really want to.

If the cset you are pruning has ancestors you want to keep, then you'll have to run hg evolve to rebase those changesets. hg evolve will do so automatically. Otherwise, you don't have to do anything.


You may clone your corrupted repo to a new one without cloning that unwanted head. Then remove old repository, move newly created clone to the original place and continue working with it. This will take some time, but you'll get a perfectly clean repository without a sign of that unwanted revision.

hg clone --rev myGoodResition myDirtyRepo myCleanRepo


I have run into this issue many times when I want to behead a head that was created in error. I always want to see it disappear off the face of the Earth.

On your local copy, get the latest and then:

  1. Find the beginning of a head you want to strip (where a new neck starts to branch off), get the revision number

  2. Strip it.


Source: TipsAndTricks.

Source: PruningDeadBranches#Using_strip.

hg --config extensions.hgext.mq= strip -n <rev>
  1. Make a trivial file update (add a whitespace to a file), commit and push.

Your repo should now have the head stripped. The last step is important as stripping doesn't create any changes you can push to your central repository. Without the last step you only have stripped the head locally.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜