Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transplant a feature (in a topic branch) to another branch?

Say I have 2 parallel long lived branches: master and experimental. And I do some work in a topic branch off of experimental, and I do this for a couple features (feature1, feature2, feature3). How would I transplant the work done in feature2 onto master?

My initial repository:

  master
    |
A-B-C
 \                              
  D-E       I       L           P
     \     / \     / \         /|
      F-G-H   \   /   \       / |
          |    J-K     \     / experimental
      feature1   |      M-N-O
              feature2      |
                         feature3

My desired repository:

                                   master
                                     |
A-B-C-----------------------------J'-K'
 \                              
  D-E       I       L           P
     \     / \     / \         /|
      F-G-H   \   /   \       / |
          |    J-K     \     / experimental
      feature1   |      M-N-O
              feature2      |
                         feature3

One way I can think of doing it is git checkout master; git cherry-pick J K but that's error prone and the topic branch might have many different commits.

I would expect it to work something like git checkout master; git <transplant-commits-in-topic-branch-onto-current-branch> feature2, but all of the rebasing commands I'm familiar with transplant all the diffs from the common ancestor (in this case A), I want to transplant just the diffs between I and K, and not mess with commit hashes.

A bit of context: I'm working on a codebase which was forked off of an original, however I'd like to contribute certain features back. And I develop all the new features in topic branches which I then merge back into my master.

like image 640
lmirosevic Avatar asked Aug 27 '13 16:08

lmirosevic


1 Answers

If you're just trying to move feature2 onto master, and 'I' and 'K' don't depend on changes made in feature1, you can transplant it with:

git rebase --onto master feature1 feature2

That syntax reads, loosely, as "Rebase feature2 onto master. It's original upstream was feature1."

Then if you merge feature2 into master, it'll be a fast-forward merge.

If, however, feature2 depends on changes in feature1 that won't be on master, the whole situation gets much more complicated, and what you want to do depends entirely on your workflow and how you relate to the parent project. Cherry-picking would work... so long as you're willing to make later merges of your feature branches complicated against the origin's master. Or you could rebase all of your branches so that feature2 applies cleanly against master, or... about a dozen other methods. It really depends on your long term plan for these branches. In general, cut feature2 (and feature3 and so on) off master if you can help it. It'll save you a lot of trouble down the line.

like image 56
Christopher Avatar answered Nov 14 '22 21:11

Christopher