I generally agree with this statement:
If I rebase my feature-branch on master branch, all the sub-branches which were on feature-branch will become stranded (based on my previous experience).
And to prevent that issue, the obvious answer is to merge the latest master
into feature-branch
, which BTW doesn't require having a local copy of master
since you can use the remote (usually called origin
):
git switch feature-branch # if not already checked out
git fetch
git merge origin/master
However, OP has added an answer as well, which contains some new information:
I did rebase the sub-branches on top of the feature-branch(with latest changes) and all looks fine.
If you are willing to rebase all of the sub-branches, then that means you can still rebase feature-branch
onto master
if you prefer that over merging. If you use rebase, you just need to keep track of the old commit of feature-branch
before it gets rebased onto the latest master
:
git switch feature-branch # if not already checked out
git branch feature-branch-old -f # -f is explained below
git fetch
git rebase origin/master
# and now for each sub-branch:
git switch sub-branch1
git rebase --onto feature-branch feature-branch-old
Note, the main difference here is for each sub-branch, instead of a basic rebase: git rebase feature-branch
, you just need to a do a "fancy" rebase to change the starting point.
Once the sub-branches are all rebased you can delete branch feature-branch-old
. Though, if you plan to continue to update feature-branch
by rebasing onto future versions of origin/master
, then you might as well keep it around until feature-branch
no longer exists. If you do this, you can simply repeat the above steps as often as you need to. This the reason I used -f
when creating feature-branch-old
. The -f
will force overwriting an existing branch if it exists, and if you repeat the above steps it will be needed.
If you generally prefer rebase over merge, I would lean towards this strategy since it's barely more work, and cleaner in the long run.
rebase --rebase-merges
(https://mcmap.net/q/12419/-what-exactly-does-git-39-s-quot-rebase-preserve-merges-quot-do-and-why), but I don't think it would work in your case, if your sub-branches were not merged back. – Trellis