Search code examples
mercurialbranching-and-mergingmercurial-extension

How to add a file from a branch to another branch in Mercurial?


I have two branches at my local repository: default and s1.

At default branch there is a file called def.txt, among others. The s1 branch has files named as set3.txt, set1.txt, etc. I would like to add set1.txt of s1 branch to default branch as well.

I have tried to use hg add set1.txt command when the working directory is at default branch. However, because default branch does not has the file, I cannot add it and it always gives me a error message such as "no set1.txt file found".

I do not want to use merge command because I do not want to merge all other files from s1 branch to default branch. I only want to add one file, set1.txt. I have tried hg transplant -b s1, too, but it seems not serving the same purpose.

So any idea how to work around this? My goal it to have these two branches look as below:

  • default: def.txt, set1.txt etc.

  • s1: set1.txt, set3.txt etc.

My machine is Red Hat Linux Workstation 6 which has Mercurial 1.7.3 and TortoiseHG 1.5.


Solution

  • You can use a trick: "revert" the file set1.txt to the branch s1. Since branches are revisions, this will work:

    $ hg revert -r s1 set1.txt