This is possible by combining the two repositories together:
Convert to a new repository with a file file that filters out everything except the file you are interested in. There is a good description at hgtip.com .
echo include my-file > filemap hg convert --filemap sourcerepo temprepo
Pull the new repository into the existing repository. This will create a whole new head that does not come from the previous set of changes. It is also described in more detail at hgtip.com .
cd destrepo hg pull --force temprepo
Finally, drain the new head.
hg merge
Here is an example on the command line. First, we create the original source repository:
$ mkdir sourcerepo $ cd sourcerepo $ hg init $ echo aaa > a $ echo bbb > b $ hg add ab $ hg commit -m first $ echo xxx > a $ echo yyy > b $ hg commit -m second
Then we create the file and convert it to a temporary repository containing only the desired file:
$ cd .. $ echo include a > filemap $ hg convert --filemap filemap sourcerepo temprepo initializing destination temprepo repository scanning source... sorting... converting... 1 first 0 second $ hg glog temprepo/a @ changeset: 1:a2c44f396733 | tag: tip | user: Laurens Holst <...> | date: Fri Oct 21 13:02:53 2011 +0200 | summary: second | o changeset: 0:68090379058b user: Laurens Holst <...> date: Fri Oct 21 13:02:41 2011 +0200 summary: first
Now we create the recipient repository:
$ mkdir destrepo $ cd destrepo $ echo zzz > z $ hg add z $ hg commit -m another $ hg glog o changeset: 0:890b51ba85c6 tag: tip user: Laurens Holst <...> date: Fri Oct 21 13:15:51 2011 +0200 summary: another
Finally, we pull the changes from the temporary repo and merge it:
$ hg pull --force ../temprepo pulling from ../temprepo searching for changes warning: repository is unrelated requesting all changes adding changesets adding manifests adding file changes added 2 changesets with 2 changes to 1 files (+1 heads) (run 'hg heads' to see heads, 'hg merge' to merge) $ hg merge 1 files updated, 0 files merged, 0 files removed, 0 files unresolved (branch merge, don't forget to commit) $ hg commit -m "merge file a from sourcerepo" $ ls az $ hg glog @ changeset: 3:7becd66c019a |\ tag: tip | | parent: 0:890b51ba85c6 | | parent: 2:dc9ac503efba | | user: Laurens Holst <...> | | date: Fri Oct 21 13:39:51 2011 +0200 | | summary: merge file a from sourcerepo | | | o changeset: 2:dc9ac503efba | | user: Laurens Holst <...> | | date: Fri Oct 21 13:02:53 2011 +0200 | | summary: second | | | o changeset: 1:2a5fa6bd9021 | parent: -1:000000000000 | user: Laurens Holst <...> | date: Fri Oct 21 13:02:41 2011 +0200 | summary: first | o changeset: 0:890b51ba85c6 user: Laurens Holst <...> date: Fri Oct 21 13:15:51 2011 +0200 summary: another
Now your file from the source repository is merged into the recipient repository, while maintaining the entire history of changes!
source share