Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to checkout a file from one branch to another in Mercurial?

Tags:

mercurial

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.

like image 242
GeekTantra Avatar asked Feb 21 '11 13:02

GeekTantra


2 Answers

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 transplanting (cherry-picking in other DVCS).

hg revert -r experimental filename could do it, not tested.

like image 121
shellholic Avatar answered Nov 13 '22 20:11

shellholic


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.

enter image description here

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

  2. Update to the target branch.

  3. Graft the source changeset. Mercurial will ask you if you want to use the changed version. Answer "c" (changed).

enter image description here

The result looks like this:

enter image description here

the file history is correctly preserved:

enter image description here

and f3.txt is not merged into the target branch.

like image 20
texnic Avatar answered Nov 13 '22 20:11

texnic