When we accept PRs to the main branch they are squashed to a single commit and the original branch is deleted.
I want to know what is the proper flow to start to work on a feature that is based on another while that other feature is waiting for the PR to be accepted.
Let's say the original feature branch is feature-1
and the new one is feature-2
. Naively, I created feature-2
from feature-1
then when feature-1
was accepted to the main branch, the history looked like this (note this is just commit order, in term of time, the commits in feature-2
were in parallel to the one in main
):
main
* GGG some other commit
* FFF squash commit of feature-1
* EEE some other commit
|
| feature-2
| DDD commit in feature-2
| /
| feature-1
| CCC commit in feature-1
| BBB commit in feature-1
|/
* AAA commit
When feature-1
was accepted to the main branch, and I tried to merge from the main branch, I got many conflicts on the changes made in feature-1
(which existed in feature-2
). Rebasing caused even more conflicts.
What is the proper way of work here? Is it to start feature-2
from main and merge/rebase feature-1? Some sort of rebase command from main?
It's ok to do that but you need to be careful not not include the commits of the original feature-1
when rebasing.... so, you need to rebase like this:
git rebase --onto upstream/master <last-commit-of-feature-1-in-feature-2> feature2
The problem is that before running the rebase you have something like this:
* HHH Merge of feature-1 (main)
|\
| * GGG squash of feature-1
|/
| * FFF second commit feature 2 (feature-2)
| * EEE first commit feature 2
| * DDD second commit feature-1
| * CCC first commit feature-1
|/
* BBB second commit main
* AAA first commit main
So, if you run a plain rebase like this
git checkout feature-2
git rebase main
git would have to consider the last common ancestor between both branches, which is BBB
and then start applying all the commits in BBB..feature-2
on top of main
. Because you squashed, git is not aware that CCC
and DDD
have already been applied on main
and so it will try to apply them separately on top of main
, hence BOOM!!!!.
So, you need to do this to come out unscathed:
git rebase --onto main DDD feature-2
Then you will get this:
* FFF' second commit feature-2 (feature-2)
* EEE' first commit feature-2
* HHH Merge of feature-1 (main)
|\
| * GGG squash of feature-1
|/
* BBB second commit main
* AAA first commit main