合并简单的上游更改时,Git子树合并报告发生冲突 [英] Git Subtree Merging reports conflict when merging a simple upstream change

查看:122
本文介绍了合并简单的上游更改时,Git子树合并报告发生冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习git 1.8.2中的子树合并。我创建了一个简单的示例来测试对第三方回购协议迁移到主项目中的更改。



我遵循 6.7 Git Tools - Subtree Merging 示例。



'sub'项目包含在'main'项目中的一个子目录中。



对'sub'项目的更改,当我尝试将更改合并到'main'项目时,git会报告冲突。

测试摘要 b
$ b


  1. 为项目'main'和'sub'(sub而不是rack)创建了回购站点

  2. 将远程命名的sub_remote添加到指向子
  3. 的main中
  4. 使用sub_branch跟踪sub_remote
  5. 更改并提交'sub'子文件中的一行项目

  6. 将sub转换为main / sub_branch

  7. 将main / sub_branch合并到main / master中。

合并因冲突而失败。

 <<<<<<<<< ; HEAD 
main
=======
主要上游变更
>>>>>>> sub_branch
main.git
sub
sub.git
tm

完整的测试脚本

 #!/ bin / sh 

#初始化空的回购
为我在主分

rm -rf $我{.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 -madded readme.md
git push origin master
cd ..
done

#将数据添加到子
ls> sub / data
cd sub
git add data
git commit -m新增数据
git push origin master
cd ..

#在主
中添加sub作为子树b 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添加子
git push origin master
cd。 。

#转换为
cd
sed -i -e's / main $ / main上游变更/'data
git commit -a - m对数据进行上游更改
git push origin master
cd ..

#将子更改合并到主
cd主
git checkout sub_branch
git pull

#merge sub_branch更改为master
git checkout master
git merge -s subtree sub_branch
cat sub / data


解决方案

read-tree 在这种情况下只是添加到你的目录中租树另一棵树的内容。这个内容就像正在创建和添加的常规文件和目录一样添加,没有历史记录结束。所有这些文件将被视为您创建它们。



当您尝试合并过程失败时,因为它看到sub_branch历史记录创建了数据文件,并且目标目录也包含由您创建的另一个数据文件。



您正在使用的页面缺少一个非常重要的步骤来使子树正常工作,以便您实际上可以将更新拉到它。



正确的示例可以在这两页中都可以看到:
https://www.kernel.org/pub/software/scm/git/docs/howto/using-merge-subtree.html
https://help.github.com/articles/working-with-subtree-merge



在你的案例中缺少的是在创建子树时正确链接历史记录:

 #创建合并记录但不改变你的树中的任何内容
git merge -s我们的--no-commit sub_branch
#把修改放到合适的子目录中
git读取树--prefix = sub / -u sub_branch

之后,您的 main repository将包含 sub 存储库的历史记录。调用失败的合并现在应该可以正常工作。调用 git log --graph 可以让你看到不同的提交是如何被合并的。


I'm getting started with learning subtree merging in git 1.8.2. I have created a simple example to test a change to a third party repo migrating into a main project.

I'm following the 6.7 Git Tools - Subtree Merging example.

The 'sub' project is included as a subdirectory in the 'main' project.

After I make a change to the 'sub' project, git reports a conflict when I try to merge the change into the 'main' project.

Test Summary

  1. Created repos for projects 'main' and 'sub' (sub instead of rack)
  2. Add remote named sub_remote to main that refers to sub
  3. Track sub_remote using sub_branch
  4. Change and commit one line in a file in the 'sub' project
  5. Pull changes from sub over to main/sub_branch
  6. Merge main/sub_branch into main/master.

The merge fails with a conflict. Merge is confused about which version of the changed line to keep.

<<<<<<< HEAD
main
=======
main upstream change
>>>>>>> sub_branch
main.git
sub
sub.git
tm

Complete Test Script

#!/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

解决方案

What read-tree is doing in this case is just adding to a directory in your current tree the contents of another tree. This contents are added just like regular file and directories being created and added, there is no history carried over. All these files will be treated as if you created them.

When you try to merge the process fails since it sees that sub_branch history created the data file, and the target directory also contain a different data file created by you.

The page you are using is missing a very important step to make subtree works properly, so that you can actually be able to pull updates to it.

The proper example can be seen in both these pages: https://www.kernel.org/pub/software/scm/git/docs/howto/using-merge-subtree.html https://help.github.com/articles/working-with-subtree-merge

What it is missing in your case is to properly link the history when you create the subtree:

# 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

After this your main repository will contain the history of the sub repository. Calls to the merge that was failing should now work properly. Calling git log --graph will let you see how the different commits are being merged.

这篇关于合并简单的上游更改时,Git子树合并报告发生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆