How to move some changeset to a new branch in mercurial - mercurial

I want to move a changeset from one branch to another. Basically, I currently have:
A -> B -> C -> D # default branch
And I want:
A # default branch
\-> B -> C -> D # some_new_branch
Where some_new_branch does not exist yet. I am used to git, so I guess there is a simple "mercurial" way I am missing.

One way is to export a patch for B,C,D; update to A; branch; apply patch:
hg export -o patch B C D
hg update A
hg branch branchname
hg import patch
To remove B,C,D from the default branch, use the mq extension's strip command.

Sounds a bit like a cherry-pick operation in git. The Transplant Extension may be what you're looking for.

With Mercurial Queue:
# mark revisions as draft in case they were already shared
#hg phase --draft --force B:D
# make changesets a patch queue commits
# (patches are stored .hg/patches)
hg qimport -r B:D
# pop changesets from current branch
hg qpop -a
#
hg branch some_new_branch
# push changesets to new branch
hg qpush -a
# and make them commits
hg qfinish -a
Without comments:
hg qimport -r B:D
hg qpop -a
hg branch some_new_branch
hg qpush -a
hg qfinish -a

Alternative to transplant or patch, you could use graft.
hg update A
hg branch branchname
hg graft -D "B:D"
hg strip B
Note that changing history is bad practice. You should strip only if you haven't pushed yet. Otherwise, you could still backout your changes.

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
...

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 - How to specify branch on commit?

I would like, when a user commit a changeset, to show a message mentioning the branch where the changeset was committed.
Example:
$hg commit -m 'Fix bug'
Changeset committed on branch bugfix
Do I actually need to modify the hg commit code or is it a quicker/simpler way of doing it?
Add to your repository's .hg/hgrc:
[hooks]
commit=echo "Changeset committed on branch `hg branch`"
ssg's answer is unfortunately not portable to e.g. Windows (because of the backticks), but this should work:
# UNIX-like
[hooks]
commit=hg log -r $HG_NODE --template "Committing on branch {branch}\n"
or
# Windows
[hooks]
commit=hg log -r %HG_NODE% --template "Committing on branch {branch}\n"

mercurial push certain revision

I have searched here, but haven't found any question related to this. I got a problem like this in mercurial:
I manage open source project in bitbucket, so i have clone of the source code in my local. But I also using that project for my own live site, so I made 2 clone of bitbucket repo
Bitbucket Repo
|
==local_clone1
|
==local_clone2-> commit1 => commit2 => commit3
(personalization) (bug fix) (add feature)
The question is, I want to push commit2 and commit3 back to local_clone1, so later on I can push to Bitbucket repo. But don't want to push commit1, since it has my personal data.
Wondering how we do that in mercurial?
This can be done without too much difficulty in this case. See Removing history in the Mercurial guide for more information.
Here's the basics of what you'll need to do:
Go to local_clone2
Get the revision number (hg tip will show you) from the current number. We'll call it 731.
hg export 730-731 > ../local_clone1/changes.diff (or wherever you like)
Go to local_clone1
hg import changes.diff
You may need to edit things manually; refer to that guide for more info in that case.
Here are a couple of options:
backout
Given a history constructed as:
hg init db
cd db
echo >file1
hg ci -Am clone # rev 0
echo >file2
hg ci -Am personalization # rev 1
echo >file3
hg ci -Am bugfix # rev 2
echo >file4
hg ci -Am feature # rev 3 <tip>
Then if the current working directory is the tip, the following commands will "undo" the personalization revision:
hg backout 1
hg ci -m backout
The advantage is history remains immutable, but shows the addition and backout of the personalization changeset.
Mercurial Queues
With the mq extension, history can be edited to remove a changeset:
hg qimport -r 1:3 # convert changesets 1-3 to patches
hg qpop -a # remove all patches (can't delete an applied patch)
hg qdel 1.diff # delete rev 1's patch
hg qpush -a # reapply remaining patches
hg qfin -a # convert all applied patches back to changesets.
The advantage is the personalization changeset disappears. The disadvantage is the changeset hashes change due to the history edit, so this should never be done to changesets that have already been pushed to others. There is also the risk of a mistake editing history.

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.