Online Book Reader

Home Category

Mercurial_ The Definitive Guide - Bryan O'Sullivan [59]

By Root 944 0
with named branches, use the hg branches command. This command lists the named branches already present in your repository, telling you which changeset is the tip of each.

$ hg tip

changeset: 0:9a972e4b5a97

tag: tip

user: Bryan O'Sullivan

date: Tue May 05 06:44:25 2009 +0000

summary: Initial commit

$ hg branches

default 0:9a972e4b5a97

Since you haven’t created any named branches yet, the only one that exists is default.

To find out what the “current” branch is, run the hg branch command, giving it no arguments. This tells you what branch the parent of the current changeset is on.

$ hg branch

default

To create a new branch, run the hg branch command again. This time, give it one argument: the name of the branch you want to create.

$ hg branch foo

marked working directory as branch foo

$ hg branch

foo

After you’ve created a branch, you might wonder what effect the hg branch command has had. What do the hg status and hg tip commands report?

$ hg status

$ hg tip

changeset: 0:9a972e4b5a97

tag: tip

user: Bryan O'Sullivan

date: Tue May 05 06:44:25 2009 +0000

summary: Initial commit

Nothing has changed in the working directory, and there’s been no new history created. As this suggests, running the hg branch command has no permanent effect; it only tells Mercurial what branch name to use the next time you commit a changeset.

When you commit a change, Mercurial records the name of the branch on which you committed. Once you’ve switched from the default branch to another and committed, you’ll see the name of the new branch show up in the output of hg log, hg tip, and other commands that display the same kind of output.

$ echo 'hello again' >> myfile

$ hg commit -m 'Second commit'

$ hg tip

changeset: 1:8928355fee43

branch: foo

tag: tip

user: Bryan O'Sullivan

date: Tue May 05 06:44:25 2009 +0000

summary: Second commit

The hg log-like commands will print the branch name of every changeset that’s not on the default branch. As a result, if you never use named branches, you’ll never see this information.

Once you’ve named a branch and committed a change with that name, every subsequent commit that descends from that change will inherit the same branch name. You can change the name of a branch at any time, using the hg branch command.

$ hg branch

foo

$ hg branch bar

marked working directory as branch bar

$ echo new file > newfile

$ hg commit -A -m 'Third commit'

adding newfile

$ hg tip

changeset: 2:f32855c6764f

branch: bar

tag: tip

user: Bryan O'Sullivan

date: Tue May 05 06:44:25 2009 +0000

summary: Third commit

In practice, this is something you won’t do very often, as branch names tend to have fairly long lifetimes. (This isn’t a rule, just an observation.)

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

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

Return Main Page Previous Page Next Page

®Online Book Reader