Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Mercurial, can I merge just some files between two branches? [duplicate]

Reading up on Mercurial, it seems to always branch and merge the complete repositories.

Is it possible to just merge some files from one branch to another? (For example I may only wish to merge in the files that fix a given bug.)

Likewise can I cherry pick some change sets, but still have a correct merge record, so if a complete merge is done later it is correct?

I am coming from a perforce “mindset” so may be thinking about this the wrong way.

like image 479
Ian Ringrose Avatar asked Nov 07 '11 16:11

Ian Ringrose


People also ask

How do I merge two branches in mercurial?

To merge two branches, you pull their heads into the same repository, update to one of them and merge the other, and then commit the result once you're happy with the merge.

What is a branch in Mercurial?

Branches occur if lines of development diverge. The term "branch" may thus refer to a "diverged line of development". For Mercurial, a "line of development" is a linear sequence of consecutive changesets. Combining a line of development into an existing one is called merging. Creating a branch.


2 Answers

Yes, Mercurial always branches and merges the whole tree. You don't have the "flexibility" that something like perforce gives you to select individual files for a merge. This is a good thing (trust me). Changesets are atomic (you can't split them) and immutable (you can't change them). Hence this needs a little bit of a mindset change.

Changesets should be targetted at one task, and one task only. If you're fixing a bug, nothing else goes in the changeset apart from the bug fix. You've then got a changeset which documents that bug fix, and you haven't got the problem of wanting to split it. It wouldn't make sense to want to. Half a bug fix is often worse than no bug fix.

When it comes to merging that there's a couple of options:

  • One school of thought says you should go back to where the bug was introduced. Fix it. Commit (making a small anonymous branch), and merge that forward onto whatever head you want it on (dev, stable, release, whatever). This isn't always practical though.
  • Another method is fixing the bug in the release branch, and then merging to the development branch. This normally works well.
  • Alternatively you could fix it at the head of your development branch, but then if you merge it onto your release branch you'll bring over all your development changes. This is where graft (new in 2.0) and the older transplant extension come into play. They allow you to "cherry-pick" a single or range of changesets from another branch and place them on another branch.
like image 194
Paul S Avatar answered Oct 06 '22 00:10

Paul S


Reading up on Mercurial, it seems to always branch and merge the complete repositories.

Yes

Is it possible to just merge some files from one branch to another? (For example I may only wish to merge in the files that fix a given bug.)

Just touch only "some files" in needed changeset and merge branch with this changeset in head with another branch or transplant in any time

Likewise can I cherry pick some change sets, but still have a correct merge record, so if I complete merge is done later it is correct?

Yes, you can transplant| any changesets to another branch, applied state will be remembered and changes will not be duplicated on final merge

like image 30
Lazy Badger Avatar answered Oct 06 '22 00:10

Lazy Badger