Create a new branch at a certain revision - mercurial

With mercurial it is easy to create a tag at a certain revision: hg tag -r <revision> <tag-name>. But how to create a branch at a certain revision?

Preface: Mercurial branches are two types:
named branch
anonymous
Named Branch
In order to get named branch BRANCHNAME, starting at REV
hg update REV
hg branch BRANCHNAME
...
hg commit
commit is a must, because
the branch will not exist in the repository until the next commit
as noted in hg help branch
Anonymous branch
hg update REV
...
hg commit
and current branch get additional head
And as a last step, use the following command to create a remote branch and push the changesets.
hg push --new-branch

You could you hg clone -r <rev>. From the command line help (run hg -v help clone):
- create a repository without changesets after a particular revision:
hg clone -r 04e544 experimental/ good/

Related

Mercurial definitive remove branch

I already read topics like How to correctly close a feature branch in Mercurial?
But I want remove branches reference.
Example
hg branch my-branch
hg commit -m "commit" --close-branch
hg branches -c
hg branches -c displays my-branch in the list. And I can't create a new branch named my-branch.
hg branch my-branch
Mercurial shows me an error :
abort: a branch of the same name already exists
Do you know how to remove branch reference definitively ?
hg help branch
...
options:
-f --force set branch name even if it shadows an existing branch
...

Branches in Git vs Mercurial

How do I return to a certain changeset (or tag) and branch out from that commit?
Once I branch out and committed changes won't affect the original branch?
|
tag-----------------
| \
branch-1 branch-2
hg update branch-1
hg branch branch-2
edit
hg commit
Note that this is named branches and there are other ways to branch in Mercurial.

can't update to mercurial branch with sub-repository

hg update develop --clean
remote: conq: repository does not exist.
abort: no suitable response from remote hg!
I would like to be able to switch to my develop branch, to undo my last 2 commits and merge the develop branch with tip or close it!
OR
just close and rename the branch, but since I can not update to it I don't know what to do.
I would like to: overwrite branch x with branch y:
hg update x
hg commit --close-branch -m 'closing branch x, will be overwriten with branch y'
hg update y
hg branch -f x
hg ci
but i can't update to x. How to fix/force this?
I used the MQ extension:
hg qinit
hg qimport -r 4:tip
hg qpop -a
hg qdelete 4.diff
hg qpush -a
hg qfinish -a
That worked but after a pull the removed stuff was back in..
But, I just created a new Branch one from tip using -f with the same name.
that works good enough for me. Can't remove the 'wrong' branch it since it is 'out of the bottle' published.

Mercurial: Convert clones to branches

I am trying to clean up some cloned repositories I have, basically converting clones into named branches.
What's the easiest way to do this?
The only way you can make them named branches would be to rewrite their history, because the name of the branch is part of the history. However, if you can survive without having the branches be named in the past (the future is fine) then you can just pull all the repos into the main one (without merging!) and then label your branches once they're in.
For example, if you have MainRepo, Feature1Repo, and Feature2Repo, you would do the following:
$ cd MainRepo
$ hg pull ../Feature1Repo
$ hg pull ../Feature2Repo
Now, if you look at the DAG in something like TortoiseHg, you'll see a couple different heads. From here, you just hg up and hg branch:
$ hg up feature1headrev
$ hg branch Feature1
$ hg up feature2headrev
$ hg branch Feature2
From here forward, you'll have your named branches.
Note: If the hg pull complains about the repositories not being related, you can do an hg pull -f, which will create multiple tails in the repository. This is usually not how true branches are, but it is sometimes useful.
You could use hg export to create a bundle of changes, and then hg import them to your new branches.
Let's say you have a common base, revA, and then two clones which each diverge from revA into revB and revC.
In the first clone: hg export -r revA -r revB > revB.patch
In the second clone: hg export -r revA -r revC > revC.patch
Now you have two patches containing all the changes made in the clones.
In the original repository:
hg up revA
hg branch branchB
hg commit
hg import revB.patch
hg up revA
hg branch branchC
hg commit
hg import revC.patch
And now you've got two named branches diverging from revA. If you have merges in the areas between revA->revB or revA->revC, I'm not sure this will preserve them perfectly.
Another alternative is to use the transplant extension. Or you could just push your clones' changes into one repo, and then move the heads that creates to new branches.

Correct way to jump back and forth between two branches?

If I create a branch:
hg branch branch-A
and commit to it:
hg commit -m "improvement A-1"
and then create a second branch:
hg branch branch-B
and commit to it:
hg commit -m "improvement B-1"
If I want to add my next change to branch-A do I simply type:
hg branch branch-A
and commit to it as before:
hg commit -m "improvement A-2"
hg branch
always creates a branch (although it warns you if the branch already exists.) To switch to an existing branch, either
hg update -r <some revision in that branch>
or
hg update <that branch>
will switch to that branch.