8

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.

ecm
  • 2,583
  • 4
  • 21
  • 29
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317

2 Answers2

11

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.
silkfire
  • 24,585
  • 15
  • 82
  • 105
Paul S
  • 7,645
  • 2
  • 24
  • 36
  • Making the distinction that bug fixes go into the release branch really gave me a moment of realisation. Thanks. – Peter Wood Feb 23 '15 at 14:43
2

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

Lazy Badger
  • 94,711
  • 9
  • 78
  • 110
  • Thanks, I did not know that changesets as well as branches could be merged. What if I only want some files on a given changeset, or some files from a number of change sets. (It would be nice if each fix was seperated well, however reallife is not always nice) – Ian Ringrose Nov 08 '11 at 09:20
  • Some files from changeset(s) isn't possible to use - you **always** apply **full changeset** to another tree. For changing changeset in repo (bad manners anyway) you can split-merge it (mq, histedit) or export as patch, edit, apply, for example – Lazy Badger Nov 08 '11 at 12:37
  • 1
    "Just touch only "some files" in needed changeset and merge branch with this changeset" - this is NOT how Mercurial works, at least in 2022. Every change in the other branch up to the selected commit will be merged, not just the files touched in the selected commit. – Violet Giraffe Jan 25 '22 at 14:57