问题
I am trying to connect two independent commit trees into one repository.
Initially I have:
A--B--C--D (master1)
E--F--G--H (master2)
\
I--J (somebranch)
These tree have no common ancestor and are independent of each other (different files and directories). I want to rebase both master2 and somebranch (may be multiple branches in fact) onto master1.
It's easy to rebase just single branch with:
#simple reabse:
git checkout master2
git git rebase master1
which would give me:
A--B--C--D (master1)
\
E'--F'--G'--H' (master2)
E--F--G--H
\
I--J (somebranch)
Preferably I would like a single command to move both (or more) branches simultanously, to get result like this:
A--B--C--D (master1)
\
E'--F'--G'--H' (master2)
\
I--J (somebranch)
回答1:
You can use a lesser-known feature called grafts to splice the histories together.
$ echo `git rev-list [E] -1` `git rev-list master1 -1` > .git/info/grafts
$ git filter-branch --tag-name-filter cat master2 [other branches]
Or if there are multiple branches involved, and you don't want to have to list them all, you can do:
$ git filter-branch --tag-name-filter cat `git show-ref --heads | awk '{print $2}'`
(for more details, see answer 161928, or blog article or advanced instructions)
回答2:
I have answered this question here and here. The short answer, use rebase with the --preserve-merges
option, but before join all branches into a common, artificial node with:
git checkout H
git merge -s recursive -Xours J # will generate a J2 node
The rebase:
git rebase --preserve-merges --onto D E^ J2
Where E^ is the parent of E and J2 the resulting node of the merge. If you use E instead of E^, node E won't be rebased.
See more detailed comments in the quoted posts.
来源:https://stackoverflow.com/questions/17106840/git-rebase-with-branches-whole-tree