问题
I started working on branchB. Which was branched off from branchA. After a few weeks, the mainline branch develop have had a lots of commits merged in. Both branches A and B are way behind.
--d1--d2--d3 ...2 weeks later... --d253 develop
\
a1--a2--a3 branchA
\
b1--b2--b3 branchB (current)
I would like to keep up to date with the develop branch. And prefer to rebase my branchB off develop at its latest commit d253. Also all commits from branchA should be ignored. This will avoid me a huge effort of resolving merge conflicts (there are a lots). Because I am not the maintainer of that branchA. I can recreate in my branchB the dependecies I need from A. Not sure if I should do that before or after the rebase.
--d1--d2--d3 ..... --d253 develop
\ \
a1--a2--a3 \
\
b1'--b2'--b3'
Q1. Is it correct to do
git checkout develop
git pull
git checkout branchB
git rebase --onto develop branchA
Q2. Let's assume the number of conflicts is quite important, about 30 files. Is rebase still a good approach compared to git merge develop
?
回答1:
Q1 - Well, it is the command that does what you show in the picture, yes. And if branch b is orthogonal to branch a, I suppose that's fine. I would hesitate to do this; is there a reason you don't want to rebase both branches up to a current d commit?
Q2 - I haven't noticed that rebase
vs. merge
makes much difference in terms of conflict resolution. The big difference would be that with the specific rebase you specified you would not include the a
changes; which means if an a
change conflicts with a d
change you don't have to deal with it. (If a b
change overlaps an a
change, I don't think this will be noticed as a conflict per se, but it could well result in code in a broken state.)
The real question is, have the b
commits been shared with other developers? If so then rebasing them away is not generally recommended as it creates problems for the other developers.
回答2:
In order to rebase branchB on develop with ONLY the commits from B. Must use rebase --onto
with 3 arguments:
git checkout branchB
git rebase --onto develop branchA branchB
Thank you to Git Tip of the Week: Rebasing Revisited Section "Rebasing onto" give an example which is similar to the scenario described in this question.
Also in git-rebase documentation. Reproduced here for convenience:
First let’s assume your topic is based on branch next. For example, a feature developed in topic depends on some functionality which is found in next.
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
来源:https://stackoverflow.com/questions/42930305/branch-off-a-branch-how-to-rebase-on-another-branch