Git 合并 - 三种方式 'git merge' [英] Git merge - Three way 'git merge'

查看:121
本文介绍了Git 合并 - 三种方式 'git merge'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 git 中模拟三路合并过程的示例,给定 为了理解这一点,我在我的 git 项目目录下创建了一个文件 testfile 并进行如下更改:

I am trying to simulate the example on three-way merge process in git given here, Below is the figure provided explaining three-way merge. To understand this, I have created a file testfile under my git project directory and made changes as below:

提交 C0

echo 'Commit #0 - master branch' >> testfile
git add testfile
git commit -m 'commit 0'

提交 C1

echo 'Commit #1 - master branch' >> testfile
git commit -a -m 'commit 1'

提交 C2

echo 'Commit #2 - master branch' >> testfile
git commit -a -m 'commit 2'

提交 C3

git checkout -b iss53
echo 'Commit #3 - iss53 branch' >> testfile
git commit -a -m 'commit 3'

提交 C4

git checkout master
echo 'Commit #4 - master branch' >> testfile
git commit -a -m 'commit 4'

提交 C5

git checkout iss53
echo 'Commit #5 - iss53 branch' >> testfile
git commit -a -m 'commit 5'

现在,为了合并来自两个分支的更改,如下图所示,我执行了 git merge 命令.

Now, in order to merge changes from both the branches, as shown in below image, I executed git merge command.

合并

$ git checkout master
$ git merge iss53

合并结果

Auto-merging testfile
CONFLICT (content): Merge conflict in testfile
Automatic merge failed; fix conflicts and then commit the result.

$ cat testfile
Commit #0 - master branch
Commit #1 - master branch
Commit #2 - master branch
<<<<<<< HEAD
Commit #4 - master branch
=======
Commit #3 - iss53 branch
Commit #5 - iss53 branch
>>>>>>> iss53

我的问题

  1. 为什么不将来自两个分支的文件内容合并到 testfile(以便 testfile 包含来自 5 个提交的 5 行)?
  2. 如何改变这个例子,使得可以研究三路合并,避免合并冲突?(可以提到任何其他情况,其中三路合并成功发生,没有任何合并冲突错误)
  1. Why doesn't it merge the file contents from both the branches to testfile (so that testfile contains 5 lines from 5 commits)?
  2. How to change this example such that three-way merge can be studied, avoiding the merge conflict? (Any other case can be mentioned wherein three-way merge occurs successfully, without any merge conflict error)

我也尝试过 git rebase,但显示合并冲突错误.

I have also tried with git rebase, but merge conflict error is shown.

推荐答案

如果在同一个文件中更改同一行,Git 无法确定如何正确解决冲突.在同一位置添加线也是同样的情况.

If you change the same line on the same file, Git cannot be sure how to properly resolve the conflict. Adding lines at the same spot is the same case.

改变你的顺序

echo 'Commit #0 - master branch' >> testfile
git add testfile
git commit -m 'commit 0'
echo 'Commit #1 - master branch' >> testfile
git commit -a -m 'commit 1'
echo 'Commit #2 - master branch' >> testfile
git commit -a -m 'commit 2'
git checkout -b iss53
echo 'Commit #3 - iss53 branch' >> testfile
git commit -a -m 'commit 3'
git checkout master
echo 'Commit #4 - master branch' >> testfile
git commit -a -m 'commit 4'
git checkout iss53
echo 'Commit #5 - iss53 branch' >> testfile
git commit -a -m 'commit 5'
git checkout master
git merge iss53

echo 'Commit #0 - master branch' >> testfile
git add testfile
git commit -m 'commit 0'
echo 'Commit #1 - master branch' >> testfile
git commit -a -m 'commit 1'
echo 'Commit #2 - master branch' >> testfile
git commit -a -m 'commit 2'
git checkout -b iss53
echo 'Commit #3 - iss53 branch' > testfile.new
cat testfile >> testfile.new
mv testfile.new testfile
git commit -a -m 'commit 3'
git checkout master
echo 'Commit #4 - master branch' >> testfile
git commit -a -m 'commit 4'
git checkout iss53
echo 'Commit #5 - iss53 branch' > testfile.new
cat testfile >> testfile.new
mv testfile.new testfile
git commit -a -m 'commit 5'
git checkout master
git merge iss53

您将获得成功的自动三向合并.

and you will get a successfull automatic three-way-merge.

这篇关于Git 合并 - 三种方式 'git merge'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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