Mercurial: merge one file between branches in one repo

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

Is it possible to merge only certain files instead of a whole set of changes?

+44
version-control dvcs branching-and-merging mercurial
Jul 03 '09 at 11:09
source share
3 answers

WARNING: such a β€œdummy merge”, as recommended by @Martin_Geisler, can really hurt you if you later want to make a true merge of the two branches. A fictitious merge will be recorded and let's say that you merged into the branch that you joined - you will not see the changes. Or, if you merged into another branch, changes in this other branch will be discarded.

If you only want to copy the 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 this file, it is useful to use "hg record" .

I just did this in my .hgignore home directory.

If both branches made changes to the file you want to save, the dirty trick would be to merge the two branches using hg merge, perhaps / possibly on another branch, check this, and then copy one file between the merge and the 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 

Worth mentioning: the way to "copy one file between branches" (or revisions, or from a merge version, 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 colleagues find it VERY confusing. "revert" == "copy" ... makes sense for some usage patterns, but not for all.

+49
Oct 26 '12 at 4:19
source share

Nope. Mercurial works on the basis of a set of changes.

But you can do a β€œ fake merge ” where you ignore incoming changes from one of the branches. Before committing, you can return the selected files to any 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.' 

Perhaps this will help you a little.

+18
Jul 03 '09 at 14:02
source share

I would just use an external tool like vimdiff to split 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. For example:

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

To do this, you need to include external tools in your .hgrc, which simply means adding these lines:

 [extensions] hgext.extdiff = 
+1
Mar 20 '15 at 11:11
source share



All Articles