Dealing with Multiple Named Branches in a Repository

If you have more than one named branch in a repository, Mercurial will remember the branch that your working directory is on when you start a command like hg update or hg pull -u. It will update the working directory to the tip of this branch, no matter what the repo-wide tip is. To update to a revision that’s on a different named branch, you may need to use the -C option to hg update.

This behavior is a little subtle, so let’s see it in action. First, let’s remind ourselves what branch we’re currently on, and what branches are in our repository.

$ hg parents
changeset:   2:f32855c6764f
branch:      bar
tag:         tip
user:        Bryan O'Sullivan <bos@serpentine.com>
date:        Tue May 05 06:44:25 2009 +0000
summary:     Third commit

$ hg branches
bar                            2:f32855c6764f
foo                            1:8928355fee43 (inactive)
default                        0:9a972e4b5a97 (inactive)

We’re on the bar branch, but there also exists an older hg foo branch.

We can hg update back and forth between the tips of the foo and bar branches without needing to use the -C option, because this only involves going backwards and forwards linearly through our change history.

$ hg update foo
0 files updated, 0 files merged, 1 files removed, 0 files unresolved
$ hg parents
changeset:   1:8928355fee43
branch:      foo
user:        Bryan O'Sullivan <bos@serpentine.com>
date:        Tue May 05 06:44:25 2009 +0000
summary:     Second commit

$ hg update bar
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ hg parents changeset: 2:f32855c6764f ...

Get Mercurial: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.