ebe*_*sle 4 git git-merge git-subtree
我开始在git 1.8.2中学习子树合并.我创建了一个简单的示例来测试对迁移到主项目的第三方仓库的更改.
我正在关注6.7 Git工具 - 子树合并示例.
'sub'项目作为'main'项目的子目录包含在内.
在我对'sub'项目进行更改后,当我尝试将更改合并到'main'项目时,git会报告冲突.
测试摘要
合并失败并发生冲突.Merge对要保留的更改行的版本感到困惑.
<<<<<<< HEAD
main
=======
main upstream change
>>>>>>> sub_branch
main.git
sub
sub.git
tm
Run Code Online (Sandbox Code Playgroud)
完整的测试脚本
#!/bin/sh
# initialize empty repos
for i in main sub
do
rm -rf $i{,.git}
mkdir $i.git
cd $i.git;
git --bare init;
cd ..;
git clone $i.git
cd $i
echo $i > readme.md
git add readme.md
git commit -a -m "added readme.md"
git push origin master
cd ..
done
# add data to sub
ls > sub/data
cd sub
git add data
git commit -m "Added data"
git push origin master
cd ..
# add sub as a sub-tree in main
cd main
git remote add sub_remote ../sub.git
git fetch sub_remote
git checkout -b sub_branch sub_remote/master
git checkout master
git read-tree --prefix=sub/ -u sub_branch
git commit -m "Added sub"
git push origin master
cd ..
# make change to sub
cd sub
sed -i -e 's/main$/main upstream change/' data
git commit -a -m "upstream change made to data"
git push origin master
cd ..
# merge sub change to main
cd main
git checkout sub_branch
git pull
#merge sub_branch changes into master
git checkout master
git merge -s subtree sub_branch
cat sub/data
Run Code Online (Sandbox Code Playgroud)
什么read-tree是在这种情况下,这样做只是增加了一个目录在当前的树另一棵树的内容.添加此内容就像创建和添加常规文件和目录一样,没有历史记录.所有这些文件都将被视为您创建它们.
当您尝试合并进程失败时,因为它看到sub_branch历史记录创建了该data文件,并且目标目录还包含data您创建的其他文件.
您正在使用的页面缺少使子树正常工作的非常重要的步骤,因此您实际上可以将更新提取到它.
在这两个页面中都可以看到正确的示例:https : //www.kernel.org/pub/software/scm/git/docs/howto/using-merge-subtree.html https://help.github.com/文章/工作与子树合并
在您的情况下缺少的是在创建子树时正确链接历史记录:
# create the merge record but not change anything in your tree yet
git merge -s ours --no-commit sub_branch
# bring the changes and place them in the proper subdirectory
git read-tree --prefix=sub/ -u sub_branch
Run Code Online (Sandbox Code Playgroud)
在此之后,您的main存储库将包含存储库的历史记录sub.调用失败的合并现在应该正常工作.调用git log --graph将让您了解如何合并不同的提交.