6

I have a large commit of many files on one branch, I need to transfer the modifications of a single file in that changeset to another branch. How can I do this? I am mostly using TortoiseHg but commandline solutions are also fine.

If I go to the changeset in TortoiseHg and select the file I can see the diffs I want to transfer, but not a way to actually apply them.

Sam Mackrill
  • 4,004
  • 8
  • 35
  • 55

2 Answers2

9

You can get the patch for just that file using:

hg log -r THEREVISIONWITHLOTSOFCHANGES -p -I path/to/justthatfile > justthatfile.patch

which you can then import on whatever branch you want by doing:

hg update anotherbranch
hg import --no-commit justthatfile.patch
hg commit
Gerhard Schlager
  • 3,155
  • 1
  • 31
  • 53
Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
2

The most basic solution is to dump the patch of the file, apply it to the current working revision, and commit it (assuming you're at the root of the repository):

$ hg up <revision-to-apply-the-patch-to>
$ hg diff -c <revision-containing-the-patch> <files-to-include> | patch -p0
$ hg ci -m "Transplanting selected changes from <revision-contain...>"

The drawback of this method is that it isn't very obvious what you've done from a revision history perspective. A good commit message helps here, but the history graph gives no hint about the process of transplanting some changes. In that case merging and reverting may be a better solution:

$ hg up <revision-to-apply-the-patch-to>
$ hg merge -r <revision-containing-the-patch>
$ hg revert --no-backup <files-to-exclude>
$ hg ci -m "Merge in changes of <files-to-include>"

Probably there are more solutions to do this -- these two came to my mind first.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Oben Sonne
  • 9,893
  • 2
  • 40
  • 61