3

Recently, I've started experimenting with Mercurial, due to the fact that it always attracted it because of its simplicity and "just works" principle. Or at least, that's how others always described it.

They also usually described it as "practically the same as git with just a few minor changes you won't notice" - only for me to discover it isn't quite so.

I'm having problem with Hg branches. Pardon me if this is an overly simple question, but in git one has a working directory and a repo (.git). In the repo one has revisions, and branches, and can jump from one to another.

I'm having trouble finding a similar model in Hg. As far as I can see, for Hg to have a "branch" one needs to clone a repo to another directory? Is there a way Hg could work just like git does - i.e. one working dir., and one repo, in which you can do things as regard to branching and revs?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Rook
  • 60,248
  • 49
  • 165
  • 242
  • 1
    I would not recommend 'named branches', the kind of branch created with the `hg branch` command, until you understand better what you're doing. Those create permanent branches that are forever a part of every version of the repository anywhere. If you're used to git branches, they probably aren't what you want. – Omnifarious Sep 29 '10 at 02:52
  • @Omnifarious - Hi. Could you maybe read my comments to Andrew and see from there what I'm stuck with (once I "branch" (split) from an older revision to side, how can I view the history by which I got there?) – Rook Sep 29 '10 at 02:56
  • Also, figuring out how extensions work and then using `hg view` or `hg glog` will help as well. – Omnifarious Sep 29 '10 at 04:18

3 Answers3

7

Mercurial supports a very rich set of ways to branch. See http://stevelosh.com/blog/2009/08/a-guide-to-branching-in-mercurial/

In brief, you can create a named branch by running

hg branch NewBranch

and switch to that branch using

hg up NewBranch

or switch back to trunk using

hg up default
Matthew Manela
  • 16,572
  • 3
  • 64
  • 66
  • Please, as so not to repeat it twice, could you read my comment to Andrew? I'd just like to see if I "got it" or am I completely walking in the fog here. +1 for the useful link – Rook Sep 29 '10 at 02:33
  • I would not recommend using the kind of Mercurial branches you create with the `hg branch` command without having a really good handle on what you're doing. I would suggest bookmarks to someone used to git branches. – Omnifarious Sep 29 '10 at 02:50
3

In Mercurial, if you go to any particular revision, you can always edit your working copy and commit, thereby making another "head." Merging works on head revisions by default. You can use hg head to see what heads are in your repository. This seems to be the most "idiomatic" way I have found branching to work in Mercurial.

Andrew
  • 14,325
  • 4
  • 43
  • 64
  • Yes, I saw something to that effect. But something has been troubling it. Say I have an initial commit (Rev0). I add a new line to it, and commit it as Rev1. Then I update to Rev0, and add a different line, and commit it as Rev2. Shouldn't "hg head" then display to me Rev2 and Rev0, since Rev0 is a parent of Rev2? – Rook Sep 29 '10 at 02:31
  • If I understood from your words, you effectivly ment to say that each time I go to an older revision, and commit from there, Hg effectivly branches from that revision? (Am I even close?) – Rook Sep 29 '10 at 02:32
  • @Rook Yes, it does. Granted, that's not necessarily the only way, as @Matthew-Manela shows you. I just hadn't had the need myself to use named branches. – Andrew Sep 29 '10 at 02:37
  • @Andrew - Is there a way when using this principle to see what the current history was? (i.e. Rev3<-Rev1<-Rev0 for example, while Rev2 was commited from Rev1, and then left alone)? – Rook Sep 29 '10 at 02:43
  • @Rook Each revision tells you what its parent or parents is or are (hg head, for example, lists head revisions, or hg log lists pretty much everything). A revision can have multiple parents when it is the result of a merge of two different heads. The revisions are specified as GUIDs. – Andrew Sep 29 '10 at 02:57
  • @Andrew - Ah, yes, that's what I thought. A little different that git, but I'll get used to it. Thanks for clearing that up for me. – Rook Sep 29 '10 at 03:16
2

Take a look at section about branches in my answer to "Git and Mercurial - Compare and Contrast" here on StackOverflow.

The information about various options available for branching in Mercurial (anonymous heads, not-propagated (I think yet still) bookmarks, and global (world-wide) commit labels aka named branches) was taken from http://stevelosh.com/blog/2009/08/a-guide-to-branching-in-mercurial/, and expanded using feedback on #mercurial IRC channel on FreeNode.

Community
  • 1
  • 1
Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • 2
    Since 1.6 bookmarks can be pushed/pulled. See the [extension wiki](http://mercurial.selenic.com/wiki/BookmarksExtension#Working_With_Remote_Repositories) and [release notes](http://mercurial.selenic.com/wiki/WhatsNew#A1.6_.282010-07-01.29) under Extensions - bookmarks. – Geoffrey Zheng Sep 29 '10 at 15:25