gitmergerebasebranching-and-merging

Proper merging of chained feature branches


Situation

I am working on features that span across multiple branches. I have a branch issue-1 that is waiting for merge with squash commits. I checked out new branch from it, issue-2 to work on it. (I need the changes from issue-1 to build up on them.) After a while the issue-1 is merged to main (squashing commits)

A--B--C---------K (main)
       \       /
        E--F--G (issue-1)
               \
                H--I--J (issue-2)
              

to be able to merge issue-2 I have to do
git rebase --onto K G issue-2 so the next merge would be smooth

A--B--C---------K (main)
       \       / \
        E--F--G   H'--I'--J' (issue-2)
       (issue-1)
              

Problem

When the accepted merge request of issue-1 provides deleting the source branch too. The history of issue-2 contains commits from issue-1, that are already merged. How can I find the correct commit for rebase --onto? I am able to find the K from merge. But where could I find the last commit of non-existent branch or what commit was the last that was merged into main? I thouth about putting it into the merge commit message, but I am not always in the charge of the merging. After the first merge (and source delete) it looks like this

A--B--C---------K (main)
       \       
        E--F--G--H--I--J (issue-2)              

Now I need to git rebase --onto K ? issue-2

Or is there a better aproach?


Solution

  • I have a branch issue-1 that is waiting for merge with squash commits.

    Since you already have the branch locally, instead of:

    git rebase --onto K G issue-2
    

    You can just use the branch name:

    git rebase --onto K issue-1 issue-2
    

    When the accepted merge request of issue-1 provides deleting the source branch too.

    That's fine, but it doesn't delete your local copy of issue-1, so you'll still have it. After you rebase issue-2 by using your local copy of issue-1, that would be a good time to delete your local copy of issue-1 since you no longer need it.

    If for some reason you deleted issue-1 before you rebased, you can use git reflog to go look back at what commit you were sitting on when you created branch issue-2.

    Also, as mentioned in the comments, your SCM Git tool may show you the history of the branch as well. Since you mentioned "merge request", that implies GitLab, and the history of each Merge Request is still available after the MR is completed.