Search code examples
gitgithubrebase

git --rebase -i a feature branch


I'm working on an open source project where I've forked their repository on github. And I have made a branch of main with some modifications, adding vscode .devcontainer. I do not wish to PR this branch onto the project I've forked. But I use it as a development environment.

Working on a new feature that I wish to PR to the project. I made a new branch from my modified branch and added the new feature here.

The following should visualize the branches where C is a commit and M is a merge

main - C1 - C2 ------------ C5---------
               \             \
        branch1 - C3 - C4 --- M1 -
                                  \
                           branch2 ---- C6 - C7

What I want to end up with, and what I thought I would end up with when doing git --rebase -i main is the following

                      branch2 ---- C6 - C7 --
                             /
main - C1 - C2 ------------ C5 ---------
               \             \
        branch1 - C3 - C4 --- M1 ----

But the commits I was presented with was only C5, C6, C7, I did not seem to rebase C6,C7 onto C1, C2, C5. so I ended up with a branch containing C1,C2,C3,C4,C5,C6,C7

I solved it by making a new branch from main and then cherry picking C6, C7 onto it. But I really thought rebase would have helped me.

When I merged C5 into branch1 should I have rebased instead so C3, C4 would be moved after C5? or instead of rebasing onto main, should I have rebased on to C2? and then skipped all commits on branch1

Suggestions, alternatives and comments is very much welcome.


Solution

  • Yo need

    git rebase --onto main branch1 branch2 
    

    From man git-rebase:

        o---o---o---o---o  master
            \
             o---o---o---o---o  next
                              \
                               o---o---o  topic
    

    We want to make topic forked from branch master; for example, because the functionality on which topic depends was merged into the more stable master branch. We want our tree to look like this:

       o---o---o---o---o  master
           |            \
           |             o'--o'--o'  topic
            \
             o---o---o---o---o  next
    

    We can get this using the following command:

    git rebase --onto master next topic