tags:

views:

123

answers:

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?

+3  A: 

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
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
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
@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
@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
@Rook Each revision tells you what its parent or parents is or are (<code>hg head</code>, for example, lists head revisions, or <code>hg log</code> 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
@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
+6  A: 

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
@Matthew - 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
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
+1  A: 

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.

Jakub Narębski
@Jakub - Thank you. I'll read it.
Rook
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
@Geoffrey Zheng: thanks, I have updated [my answer](http://stackoverflow.com/posts/1599930/revisions)
Jakub Narębski