3

My branching looks like this

M1 --- M2 --- M3 --- ............. --- M50 --- M51
               \
                \
                 P1 --- P2 --- P3 --- P4
                                \
                                 \
                                  C1 --- C2 --- C3

Here M is master commits P is parent branch commits C is child branch commits

I was developing a feature(F1) and created a branch(Parent) and had commits until P3. At this point I started working on new feature(F2), but since I needed P3 commit changes to work on new feature I created a branch(child) at P3. I got one review comment for feature F1 and had committed P4 in parent branch.

There was no conflict so I rebased the child branch to get something like this.

M1 --- M2 --- M3 --- ............. --- M50 --- M51
               \
                \
                 P1 --- P2 --- P3 --- P4
                                       \
                                        \   
                                         C1 --- C2 --- C3

Now my parent branch commits are squashed and merged with master at M52. Here I am confused on what happens with my child branch. I guess it is like this.

M1 --- M2 --- M3 --- ............. --- M50 --- M51 -- M52(contains squashed commits of P1-P4)
               \
                \
                 C1 --- C2 --- C3

If my guess is correct then what should be the right command to achieve something like this? Please let me know and next step if my guess is wrong too.

M1 --- M2 --- M3 --- ............. --- M50 --- M51 -- M52
                                                        \
                                                         \
                                                          C1 --- C2 --- C3

I believe it is again a rebase with onto, but I am not sure what it should be exactly? Plenty of teams work and merge their branches on master, so I don't want to take risk with wrong commands myself.

Thanks in advance.

  • Your guess for the 3rd schema looks wrong. Commits `P1` to `P4` still exist and are part of your `F2` branch history. You'd obtain what you drew if you'd rebase on `M3`. But as you half guessed it already, you'll probably prefer rebasing on master (which is at `M52`) – Romain Valeri Oct 28 '21 at 09:52
  • Thanks, but what command should I use to rebase to master(M52). Do I need to use the keyword --onto, otherwise I may get conflicts. Right? – Trilokinath Modi Oct 28 '21 at 12:15
  • Note that *branches* do not have parent/child relationships; instead, *specific commits* have parent/child relationships. (The word *branch* is ambiguous, but whichever meaning we take, we get some sort of problems here.) This doesn't really help *answer* your problem but it's a good idea to be careful about wording to help *pin down* your problem. :-) – torek Oct 29 '21 at 00:23
  • (If I understand what you want—and that's a pretty big "if"—LeGEC's answer is the right one.) – torek Oct 29 '21 at 00:25

1 Answers1

2

After the "squash & merge" of your parent branch, your history will look like this :

M1 --- M2 --- M3 --- ............. --- M50 --- M51 --- M52
               \
                \
                 P1 --- P2 --- P3 --- P4  # <- the 'Pxx' commits still exist
                                       \
                                        \   
                                         C1 --- C2 --- C3

The rebase command you are looking for is :

git rebase --onto M52 P4 child

After that, you will have to force push your child branch to update it on the remote repo :

git push --force-with-lease origin child
LeGEC
  • 46,477
  • 5
  • 57
  • 104