9

I read this document: A Guide to Branching with Mercurial, specifically the section titled Branching with Bookmarks.

It says:

Now you’ve got two bookmarks (essentially a tag) for your two branches at the current changeset.

To switch to one of these branches you can use hg update feature to update to the tip changeset of that branch and mark yourself as working on that branch. When you commit, it will move the bookmark to the newly created changeset.

I tried this, but it ended up moving both bookmarks at the same time.

Is that guide wrong, outdated, or did I do something wrong? Note that I know that having bookmarks on separate branches only moves the bookmark related to the branch I'm currently working on, but that guide (which a lot of people says is the definite guide to this) specifically says the above text, which indicates that it should've worked by "telling" Mercurial which bookmark (branch) I'm working on.

Testing shows otherwise though.

Any ideas?

Example:

> hg init
> echo 1 >test.txt
> hg commit -m "initial" --addremove
adding test.txt

> hg bookmark main
> hg bookmark feature
> hg log
changeset:   0:c56ceb49ee20
tag:         feature
tag:         main
tag:         tip
user:        Lasse V. Karlsen <lasse@vkarlsen.no>
date:        Tue Nov 30 23:06:16 2010 +0100
summary:     initial

> hg update feature
0 files updated, 0 files merged, 0 files removed, 0 files unresolved

> echo 2 >test2.txt
> hg commit -m "feature 1" --addremove
adding test2.txt

> hg log
changeset:   1:9f2f5869b57b
tag:         feature                             <---- both were moved
tag:         main                                <----
tag:         tip
user:        Lasse V. Karlsen <lasse@vkarlsen.no>
date:        Tue Nov 30 23:06:45 2010 +0100
summary:     feature 1

changeset:   0:c56ceb49ee20
user:        Lasse V. Karlsen <lasse@vkarlsen.no>
date:        Tue Nov 30 23:06:16 2010 +0100
summary:     initial
Community
  • 1
  • 1
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825

2 Answers2

17

If I got you right, you want only the bookmark you've updated to move on the next commit. For this purpose the bookmarks extensions has the track.current option.

From BookmarksExtension:

By default, when several bookmarks point to the same changeset, they will all move forward together. It is possible to obtain a more Git-like experience by adding the following configuration option to your .hgrc

[bookmarks]
track.current = True

In your example, this would keep the main bookmark at revision 0.

If the track.current option is enabled, the currently active bookmark is annotated with an asterisk in the output of hg bookmarks.

UPDATE: Since Mercurial 1.8 the default behavior is to only move the current bookmark, i.e. the above mentioned option is not needed anymore [1].

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Oben Sonne
  • 9,893
  • 2
  • 40
  • 61
  • Perhaps that was the default behavior at some point, and was changed? Anyway, you're right, with that configuration it behaves exactly like the guide portrays. Thanks! – Lasse V. Karlsen Dec 02 '10 at 11:59
  • No, the `track.current` option has always defaulted to False. There has been some recent discussion about making the bookmark command a core command and in relation to that it was suggested that `track.current` should begin defaulting to True. – Martin Geisler Dec 03 '10 at 14:20
  • 3
    +1 for the helpful answer, but it leads to an obvious question: when is track.current *not* appropriate? I can't imagine a situation where I have multiple bookmarks and I want them *all* to move forward upon a commit. – Marcelo Cantos Jan 05 '11 at 09:28
  • @Marcelo: I also cannot think of a real life situation where multiple bookmarks should move. You should post this issue as a new question! – Oben Sonne Jan 05 '11 at 18:37
  • @MarceloCantos: local tags. You just want to, well, bookmark a commit in your local repository but don't want to make it a permanent tag. A possible reason being that you only use it temporarily for orientation. But yes, a rare case. – DanMan Mar 28 '14 at 21:44
3

If you read the description of the BookmarksExtension, it says:

Bookmarks are references to commits that are automatically updated when new commits are made.

and:

Since bookmarks are automatically updated when commiting to the changeset they are pointing to, they are especially useful to keep track of different heads.

In your case, you only had one head in the repository at the time you created both bookmarks. If you use a sequence like the following, it should work as you expect:

hg init foo
# edit, edit, edit
hg commit -A -m "root"
hg bookmark main
# edit, edit, edit ...
hg commit -m "main 1"
hg update 0
# edit, edit edit
hg bookmark feature
hg commit -m "feature 1"
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Niall C.
  • 10,878
  • 7
  • 69
  • 61
  • 1
    So basically you're saying that the guide is wrong? That is my question after all. I know that individual bookmarks, like your example, works as expected. But that guide seems to say that if you execute `hg update feature`, only that bookmark will be advanced. – Lasse V. Karlsen Nov 30 '10 at 23:05
  • @Lasse: no, I think that the document leaves out some details for brevity; the author _does_ say to read more about bookmarks before using them. – Niall C. Nov 30 '10 at 23:13
  • 1
    I guess the author of the guide (silently) assumed that the *track.current* option of the bookmarks extension is enabled (see my related answer). – Oben Sonne Dec 01 '10 at 12:52