问题
I have a two branches that I keep on top of upstream/master. One branch has the other as an ancestor so they form a line.
U1 (upstream/master)
\
A -- B (fixes)
\
C -- D (features)
Later, upstream/master moves forward...
U1 -- U2 (upstream/master)
\
A -- B (fixes)
\
C -- D (features)
... and I want to rebase both branches on top.
U1 -- U2 (upstream/master)
\
A' -- B' (fixes)
\
C' -- D' (features)
I know two ways to do it, each with drawbacks.
git rebase upstream/master fixes
git rebase fixes features
These two commands sometimes work and sometimes give me merge conflicts on the second command.
git rebase upstream/master features
# figure out the hash code of the new commit corresponding with B'
git branch -f fixes <sha of B'>
This only involves a single rebase, but moving the branches is tedious and error prone.
I looked into rebase --preserve-merges, but that doesn't seem applicable because there are no merge commits.
Is there a better way to accomplish the rebase?
回答1:
One way to avoid the merge conflicts is to move the branches from the pre-rebase history to the post rebase one. This is admittedly terrible because it requires figuring out the new version of each commit, but might be faster than merging unnecessarily.
eg.
git rebase upstream/master features
U1 -- U2 (upstream/master)
\
A' -- B'
\
C' -- D' (features)
git branch -f fixes (SHA for B') # Change where the branch is pointing.
U1 -- U2 (upstream/master)
\
A' -- B' (fixes)
\
C' -- D' (features)
来源:https://stackoverflow.com/questions/35618852/easiest-way-to-rebase-multiple-branches-in-a-single-lineage