我有两个项目,A和B.他们分得很差,因为A必须在B之前运行一部分而在B之后运行另一部分。所以我决定将它们合并。 B的内容被复制到A的工作目录中,已经提交,并继续从那里开始工作。
当然,B的历史并没有保留在复制粘贴中。我想在它被遗忘之前解决这个问题并在以后咬人。
如何修改A的存储库历史记录以显示B存储库的导入?
我想我需要引入一个新的根节点,将B的内容复制到该根节点上,然后将'添加新文件'提交替换为'将根节点B合并到A'。我不知道怎么做这些。
答案 0 :(得分:2)
听起来你想做subtree merge。
答案 1 :(得分:2)
您必须先在复制粘贴之前对提交进行子树合并,然后将以下提交重新绑定到合并的结果上。确保你记得rebase的--preserve-merges开关,否则这两棵树最终会交错而不是合并。
命令:
'(First, write down the ids of the current head and the commit before the copy paste)'
'(Second, do this on a copy of the repository in case you make a mistake)'
#Set the head to the commit before the copy paste (and checkout that commit)
git reset idOfCommitBeforeCopyPaste --hard
#Merge-in the master branch from the other repository, using magic
git remote add -f otherName other/repository/path
git merge -s ours --no-commit otherName/master
git read-tree --prefix=otherName/ -u otherName/master
git commit -m "Subtree merge from other"
'(Now you many want to move some files and do a commit)'
'(Write down the id of the current head, which is the result of the merge)'
#Apply all following commits to become equivalent to the up-to-date commit, and checkout
git reset headIdFromBeforeWeStarted --hard
git rebase mergeCommitId --preserve-merges
'(You may have conflicts; resolve them in the normal way then rebase --continue)'
'(To verify you did not change the present, compare the resulting working directory)'