Mercurial: Merging one file between branches in one repo

Version ControlMercurialDvcsBranching and-Merging

Version Control Problem Overview


When I have two branches in Hg repo, how to merge only one file with another branch, without having all other files from changeset merged?

Is it possible to merge only certain files, instead of whole changeset?

Version Control Solutions


Solution 1 - Version Control

WARNING: such a "dummy merge", as is recommended by @Martin_Geisler, can really mess you up, if later you want to do a true merge of the two branches. The dummy merge will be recorded, and say that you merge into the branch you did the dummy merge to -- you will not see the changes. Or if you merge into the other branch, the changes on that other branch will be undone.

If all you want is to copy an entire file from one branch to another, you can simply do:

   hg update -r to-branch
   hg revert -r from-branch file
   hg ci -m 'copied single file from from-branch to to-branch

If you want to select different parts of that file, then "hg record" is useful.

I just did this on my home directory .hgignore.

If both branches have made changes to a file that you want to keep, a dirty trick would be to create a merge of the two branches using hg merge, possibly/probably on still another branch, check that in, and then copy a single file between the merge and the to-branch:

   hg update -r to-branch
   branch merge-branch
   hg merge -r from-branch
   hg ci -m 'temp merge to be discarded"
   hg update -r to-branch
   hg revert -r merge-branch single-file
   hg ci -m 'merged single-file from from-branch to to-branch"
   hg strip merge-branch

It is worth mentioning: the way to "copy a single file between branches" (or revisions, or from revision to merge, or....) is "hg revert". I.e.

   hg update -r Where-you-want-to-copy-to
   hg revert -r Where-you-want-to-copy-from file-you-want-to-copy
   ...
   hg ci

For some reason I, and some of my coworkers, find this VERY confusing. "revert"=="copy" ... makes sense for some usage patterns, but not all.

Solution 2 - Version Control

Nope. Mercurial works on a changeset basis.

But you can do a "dummy merge" where you ignore the incoming changes from one of the branches. Before you commit you could then revert selected files to whatever state you want:

% HGMERGE=internal:local hg merge     # keep my files
% hg revert --rev other-branch a.txt  # update a.txt to other branch
% hg commit -m 'Dummy merge to pick a.txt from other-branch.'

Maybe that will help you a bit.

Solution 3 - Version Control

I would just use an external tool like vimdiff to diff the two files that I want to merge and then merge them. The advantage of this is that you can do selective editing on parts of the file. E.g:

hg update -r branch-merging-to
hg extdiff -p vimdiff -r branch-merging-from file-I-am-merging

To do this you need to enable the external tools in your .hgrc, which just means adding these lines:

[extensions]
hgext.extdiff =  

Solution 4 - Version Control

One fairly clean way of getting the desired result is to do it in two steps: first use graft, then second use histedit.

Say this is the starting point and you need to select some portions of C and D to "merge" after E:

A---B---C---D
      \
       -E

Then you would graft C and D on top of E:

A---B---C---D
      \
       -E--C'--D'

Then use hg histedit to edit C' and D'. During the edit you can make any changes you want, but in this case you would just revert any unwanted files, (or even portions of them).

(Note that histedit edit works by temporarily updating your working folder to match the content of the given changeset as though it were not committed yet. So you can easily revert unwanted files and then hg histedit --continue which will effectively replace the edited changeset.)

So the final result would be:

A---B---C---D
      \
       -E--C''--D''

Where the '' revisions were modified as required.

I would say this approach is more beneficial when you have large changesets that probably should have been multiple smaller commits in the first place; this approach allows you to "disentangle" only the parts that you need. Using this for just a single file would be fine but could be overkill.

Solution 5 - Version Control

If you are using an IDE:

  1. Merge the old branch with new branch
  2. Go inside the the IDE and remove the unwanted changes
  3. Generate the diff file
  4. Update and clean the new branch
  5. Apply the diff in the new branch

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoxView Question on Stackoverflow
Solution 1 - Version ControlKrazy GlewView Answer on Stackoverflow
Solution 2 - Version ControlMartin GeislerView Answer on Stackoverflow
Solution 3 - Version ControlToby SearleView Answer on Stackoverflow
Solution 4 - Version ControlStayOnTargetView Answer on Stackoverflow
Solution 5 - Version ControlRaja VooraView Answer on Stackoverflow