Sam Mackrill
Sam Mackrill

Reputation: 4051

In Mercurial how do I extract a single file's changes from a Changeset to apply to another branch?

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.

Upvotes: 6

Views: 2942

Answers (2)

Ry4an Brase
Ry4an Brase

Reputation: 78330

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

Upvotes: 9

Oben Sonne
Oben Sonne

Reputation: 9953

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.

Upvotes: 2

Related Questions