28

I am trying to do something very simple: create a new branch. But I messed up. Where did I make the mistake, and how do I fix it?

I am the only user of Mercurial. I had revision 54 committed and pushed to remote repository. I wanted to create a branch based on revision 53, so I updated my local copy to revision 53, made changes, and committed (ignoring the warning about "it's not the head"). Then when I am trying to push to remote repository, it says

abort: push creates new remote head

Maybe I needed to tell Mercurial that I want to create a new branch? If so, how and at what point?

Thanks!

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
max
  • 49,282
  • 56
  • 208
  • 355
  • 1
    This is essentially a duplicate of http://stackoverflow.com/questions/6948574/why-is-a-new-branch-flag-needed but asked in reverse. – Tim Delaney Feb 01 '12 at 06:37

3 Answers3

36

You tell Mercurial that it can go ahead with

$ hg push --force

You need to force it since multiple (unnamed) heads are normally discouraged. The problem with them is that people that clone the repository wont know which one to use. But since you're the only user you can just go ahead and push.

The alternative is to use a named branch (with hg branch) and then you'll use

$ hg push --new-branch

to allow the creation of a new branch on the remote. Named branches have the advantage that they make it easy to distinguish the two branches. They have the disadvantage that they are permanent. Permanent means that you cannot remove the branch name from the changesets on the branch — the name is literally baked directly into the changeset.

Bookmarks provide a way to have non-permanent branch names, see hg help bookmarks.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
  • 1
    Although I'm the only developer now, I want to keep to good practices, so if named branches are better, I'd rather use them. What's bad about the *permanent* branches? I mean, eventually I can merge them and they will become one again, right? – max Jan 31 '12 at 19:47
  • 1
    Also, is there a way to tell Mercurial, retroactively, that an existing revision (which I didn't name a new branch initially) is in fact a separate branch? – max Jan 31 '12 at 19:47
  • Yes, you can definitely merge the two heads back to just one head — regardless of using named branches or not. I've updated the answer with a bit about what *permanent* means for named branches. – Martin Geisler Jan 31 '12 at 20:00
  • @max: you should probably use a bookmark to label your two heads. Bookmarks are a standard feature now and can be pushed or pulled between repositories. – Martin Geisler Jan 31 '12 at 22:19
  • We use Sourcetree to do this, any idea how to force without the command line? – Leon Gaban Sep 11 '13 at 15:58
  • @max named branches aren’t bad. I perceive them as a very useful feature which has the sole complexity that creating a new branch with the same name as an old branch gives a warning. If you want to finish a branch, you can close it with `hg commit -m "note" --close-branch`. – Arne Babenhauserheide Dec 04 '13 at 08:55
2

Another reason for this error: probably there are some UNMERGED changes form the central repo in your default branch.

hg up default
hg merge
hg ci -m "Merge"
hg pus
Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
0

I did this. Using TortoiseHg ... this is how I fixed it:

In settings, I enabled the Strip extension then right clicked the branch i did not want, Modified History - strip. If you have pushed, then it needs to be stripped from all other repositories, including workmates who have pulled your unwanted branch.

An alternative is to merge the unwanted branch into your main branch, but do not take any of the changes from that branch - I am unsure of how that mechanism works.

Ruskin
  • 5,721
  • 4
  • 45
  • 62