Search code examples
gitrebase

Git Rebase for feature X on feature Z after merge on staging


How to Rebase right for the following problem, is rebase the right Solution? We use rebase for default branching strategies and the only merge happens in MR normaly.

 A--B--C (staging)
        \
         D--E--F (x-feature)
                \
                 G--H--I (y-feature)

I've a staging branch, where I've created a x-feature branch and then I've created my MR for this branch but I need the implemented features and so I've created an other y-feature from my x-feature branch.

And When I am right, when the MR is merged it should look like the following and my y-feature Hangs around, because the MR-Branch gets deleted after MR.

 A--B--C--D--E--F (staging)
          
               \
                G--H--I (y-feature)

How do I get my y-feature reattached to my staging branch, without data loss?


Solution

  • As described, everything will work just fine. Branches are just pointers to commits (they don't point to other branches), so when the x-feature branch is merged into the staging branch, the y-feature branch will still point to commit I, and G's parent commit will still be commit F. At that moment both staging and x-feature will be pointing to commit F, and if the x-feature branch is deleted it doesn't matter.

    Side Note: if branch x-feature is re-written for some reason before it is merged into staging, then commit F may no longer exist on the staging branch. In that case you probably would want to rebase y-feature onto the new staging branch, with a command similar to this:

    git rebase --onto origin/staging F
    

    That would replay commits G, H, and I on top of the updated staging branch.