Rename a committed to branch in Mercurial

2019-04-06 12:17发布

问题:

Mercurial - What are the steps involved to accomplish the rename of a branch after it has been created and committed to (both locally and in the central repo).

For example, I've created a branch called Zelda and then committed and pushed to a central repository. I now want to change the name of the branch to Triforce.

Is there an extension that accomplishes this? What might be the long way in addition to any extension solution?

回答1:

The short answer is no.

The long answer is, Mercurial branches are names attached to changesets. If you have several changesets committed to the branch zelda and given away (i.e., pushed to the central repo), there's no way you can rename that branch without re-creating these changesets, which means rewriting history.

Even if you strip those changesets both in you repo and in the central repo, then re-create them as belonging to the branch triforce, and push the “renamed” branch again, all of your collaborators will end up having two copies of the changesets, one set on branch zelda, another on branch triforce.

So yes, what you should do is described in https://stackoverflow.com/a/7245187/67988. To quote from there, adjusted to your branch names, i.e. zelda is the old branch to be closed and triforce is the new branch to be created.

hg update zelda
hg commit --close-branch -m "mgmt: Close branch zelda"
hg branch triforce
hg commit -m "mgmt: Create branch triforce"
hg push --new-branch

P.S. If no one else pulled from the central repo yet, you can try stripping zelda from there. Or, if you have access to all other devs' PCs (assuming a controlled environment), and really want to go one very dangerous road, you can strip this branch from all the repos. This is definitely not considered good practice, and can only be used as a last resort measure.



回答2:

The mutable-branches extension can give the illusion of having renamed a branch.