How to checkout a single file from one branch to another in Mercurial?
Basically I want to copy a single file from a branch experimental
to another branch production
.
You can show any file at any revision with hg cat -r experimental filename
. But this kind of behavior looks like a debugging patch, in which case I would consider transplant
ing (cherry-picking in other DVCS).
hg revert -r experimental filename
could do it, not tested.
hg revert
as suggested by @shellholic will indeed create a file from one branch in another branch. However the file will look as freshly added in history. Its actual history is not shown in the target branch.
I find grafting a better alternative.
Let's assume you have 2 branches, and you have created and worked on file f2.txt in the side branch. You were also working on the file f3.txt in the same branch at the same time.
Create another changeset in the source (side) branch, this changeset containing only the file you are interested in. If you already have such changeset, use it, no need to create a new one.
Update to the target branch.
Graft the source changeset. Mercurial will ask you if you want to use the changed version. Answer "c" (changed).
The result looks like this:
the file history is correctly preserved:
and f3.txt is not merged into the target branch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With