4

If I start with a structure like follows on my remote:

A-B-C-D-F-G  master
   \
    E-H-I  branch

And I clone branch and make change 'J' (& commit and push to remote branch) how can I merge 'J' to master without bringing down 'H' and 'I'? Can it be done just by pushing change 'J' or do I need to switch to a local repo tracking master and merge the local 'J' change and push that to master?

A-B-C-D-F-G-J  master
   \       
    E-H-I-J  branch
BenG
  • 130
  • 6

2 Answers2

3

To make a history that reflects reality and so save yourself potential trouble downstream,

git checkout -b Jbranch B
git cherry-pick J
git checkout -B branch I
git merge Jbranch
git checkout -B master G
git merge Jbranch
git branch -d Jbranch

Producing

A..B..C..D..F..G..J''  master
   |             /
   |\........J'.+
   |             \ 
    \....E..H..I..J    branch

The 'J' commit's tree in the old and new 'branch' tip will be identical, just with the explicit history this time.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • Any idea why cherry-pick(ing) J directly from branch to master would cause me later merge troubles. Surely the merge is capable of seeing C-D-F-G-J and H-I-J have J in common and only merge H & I. Most other version control systems do this, whats different with Git? – BenG Apr 25 '13 at 07:53
  • 1
    It's a pure safety play, just like any vcs git can identify the cherry-pick because the two commits apply patches that look the same, but any system that works on detecting similarities like this can be fooled. – jthill Apr 25 '13 at 09:35
1

You can cherry-pick the change J. Cherry-picking only copies a particular commit to a branch.

$ git checkout master
$ git cherry-pick J

Note that commit J will now be duplicated (it'll exist on each branch).

mipadi
  • 398,885
  • 90
  • 523
  • 479