Mercurial: Create a branch without having to make a change first

Neil Barnwell picture Neil Barnwell · Jan 25, 2011 · Viewed 16k times · Source

I heard that the only way to create a branch in a Mercurial repository is to make changes in the working copy, then commit them to a new branch.

In Subversion, I can create a branch without having to make changes (by copying trunk to a path under tags) - is it possible to do this in Mercurial as well?

I've currently only seen TortoiseHg, so it's possible this can only be done via the command-line tool and I don't know it.

My workflow for this is:

  • Create feature branch.
  • Do some work in that feature branch.
  • Create a release candidate branch.
  • Merge feature(s) to release candidate branch.
  • Deploy, test, fix deploy, test, fix release candidate branch.
  • Merge release candidate branch to trunk.

Many thanks in advance.

Answer

Lasse V. Karlsen picture Lasse V. Karlsen · Jan 25, 2011

It depends on what you mean by branch.

A named branch can be created by giving it a name and then committing, it'll end up as a new changeset in the history, but you don't need to explicitly change any of the files in the working folder to be allowed to commit.

hg branch NEWNAME
hg commit -m "Created branch NEWNAME"

You can also do this using the TortoiseHg dialog.

However, if you want to create another unnamed branch, ie. just another head, then yes, you need to change something. And really, why would you want to just create an empty changeset with no changes? Just to signal that "this is where I'll place my branch when I have some changes"?