3

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.

brandizzi
  • 26,083
  • 8
  • 103
  • 158
Cassie
  • 1,179
  • 6
  • 18
  • 30
  • Does this answer your question? [Mercurial: Merging one file between branches in one repo](https://stackoverflow.com/questions/1078881/mercurial-merging-one-file-between-branches-in-one-repo) – StayOnTarget Apr 27 '20 at 14:36

2 Answers2

7

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
brandizzi
  • 26,083
  • 8
  • 103
  • 158
0

hg revert is a cute trick. I'd be more inclined to use hg cat -r s1 set1.txt > set1.txt however, that more explicitly does what you'd expect. I don't think ultimately there's any difference between the two commands.

Intuitively, you're trying to merge a single file from one branch into your current branch. A quick google turned up this article exploring this concept in a DVCS: http://josefbetancourt.wordpress.com/2011/01/13/merge-single-file-hg/

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • The `hg revert` hack has the advantage of preserving the history of the file. If you do `hg log set1.txt` on `default` branch it will show all previous commits. This is not as killer as some people find but it can be useful. – brandizzi Sep 08 '12 at 22:45