Search code examples
gitrebase

Git: How to rebase a feature branch that is based on another feature branch onto master?


My git history looks like this:

 A     B    main
 |     |    | 
 *     |    |
 *     |    |
 *     |    |
   \   |    |
     \ |    |
       *    |
       *    |
        \   |
          \ |
           \|

What I want something like this:

B   main A
|  |   |
|  |   |
|  |   |
|  |   |
|  |   |
|  |   *
*  |   *
*  |   *
\  |  /
 \ | /

When I tried rebasing A onto main, it rebased B. The main issue is that I have things on A, that are dependent on B, but I would like to keep those changes on B. Is this possible?

Thanks in advance.


Solution

  • So redrawing with named commits so it is possible to refer to them individually.

      a3 - <A>
       \
        a2
         \
          a1
           \  b2 - <B>
            \ |
             b1 
              \ 
               m35 - <main>
                |
               m34
                |
               ...
    

    What you want is

      a3' - <A>
       \
        a2'
         \       b2 - <B>
          a1'     |
           \     b1
            \   /
             m35 - <main>
              |
             m34
              |
             ...
    

    where a1' is commit a1 rebased on top of main but excluding the changes from commit b1, etc.

    This is as Phillip commented a prime use case for rebase --onto, namely in this case

    git rebase --onto main b1 A  # where "b1" is the actual commit id for commit b1
    

    PS When running (non-interactive) rebase you should exclusively use the 2 or 3 argument form.