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:

  • GIT clone not working
  • error: Untracked working tree file
  • Local Repository in Xcode 4
  • Why do people put the .env into gitignore?
  • Git: trailing whitespace causes file to remain in modified state
  • Creating a New Git Repo from existing files on Windows
  •   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.

  • Does git log --branches work?
  • Diff in Emacs between version of file in working dir and previous commit in git repo
  • Working directory diff with Git Extensions
  • Can the “post-receive” hook in gitlab-shell still be used for a custom hook?
  • git: what is the correct merging or rebasing workflow to modify a maintenance branch and apply those patches to another branch?
  • Git: Can't delete remote branch permanently
  • One Solution collect form web for “How to transplant a feature (in a topic branch) to another branch?”

    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.

    Git Baby is a git and github fan, let's start git clone.