Mercurial: Can I rename a branch?

Update to the stiging branch and create a new branch off of it. Then close the old branch.

In summary:

hg update stiging
hg branch staging
hg commit -m"Changing stiging branch to staging."
hg update stiging
hg commit --close-branch -m"This was a typo; use staging instead."
hg push --new-branch

Make a new branch called "staging" and forget the other...


For future readers: With the rebase extension, you can make a new branch with the same parent as stiging and move the entire branch history to it, like this:

hg update -r "parents(min(branch('stiging')))"
hg branch staging
hg commit
hg rebase --source "min(branch('stiging'))" --dest staging

This assumes that stiging has only one parent. Of course you can just use explicit revision numbers instead.

Note 1: If branch stiging includes merges with other branches, I think that this will preserve them, as long as staging and stiging have the same parent. But I'd certainly double-check.

Note 2: Since this edits the history, the old branch won't simply disappear from cloned repositories (see the rebase documentation). Unless everyone can clone anew, it might not be a very practical solution for a large group.

Note3/Edit (courtesy of @JasonRCoombs): Now that phases are standard in mercurial, rebase will refuse to modify changesets that have already been pushed. Either fool it by changing the phase back to draft (with hg phases), or let the old branch stay where it is, and just make a properly named copy (e.g., with `hg rebase --keep').