Git merge - Three way 'git merge'

╄→гoц情女王★ 提交于 2021-01-27 17:10:19

问题


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:

Commit C0

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

Commit C1

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

Commit C2

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

Commit C3

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

Commit C4

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

Commit C5

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

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

Merge

$ git checkout master
$ git merge iss53

Merge Result

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

My questions

  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)

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


回答1:


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.

Change your sequence from

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

to

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.



来源:https://stackoverflow.com/questions/43846215/git-merge-three-way-git-merge

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!