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

16,685

Solution 1

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"?

Solution 2

You might want to check out the bookmarks extension (which is soon to be part of core Mercurial).
If you're familiar with how branches work in git, then it's almost the same.

And just like branches in git, you may create a bookmark without committing anything.

$ hg bookmark my-feature

Solution 3

After creating the branch dont forget to push it with:

hg push --new-branch
Share:
16,685

Related videos on Youtube

Neil Barnwell
Author by

Neil Barnwell

I'm an Application Developer and Software Architect for a wide variety of software solutions including websites and desktop applications. Most recently focussed on a bespoke warehouse management system using C# 3.5 and SQL Server 2008. Since my move to .NET I've become active in the community, attending monthly usergroup meetings and various conferences. I've even made a foray into speaking at usergroups about topics I am passionate about. While I love experimenting with new tech and have a hobby project hosted on CodePlex, my current focus is less on specific new technologies and more on good principles and techniques. When not at work I'm a family man, biker, amateur photographer, guitarist and of course, software developer.

Updated on May 17, 2022

Comments

  • Neil Barnwell
    Neil Barnwell about 2 years

    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.

    • Ry4an Brase
      Ry4an Brase over 13 years
      There's a growing consensus that named branches aren't a good fit for feature-level changes in Mercurial because they can't ever be deleted, only hidden. Many folks prefer reserving named branches for long lived concepts like 'stable', and 'expirimental' and use either anonymous branches, bookmarks, or clones-as-branches for features and bugs. Here's a great write-up: stevelosh.com/blog/2009/08/a-guide-to-branching-in-mercurial
  • Neil Barnwell
    Neil Barnwell over 13 years
    To answer your question, it's where the next change will actually be the result of a merge. TortoiseHg wouldn't allow the result of a merge to be committed anywhere other than one of the two "inputs" to the merge, unless we created a branch first. However, the named branch process you described above solves my problem very nicely, thanks.