gpt4 book ai didi

git - 为什么会导致 merge 冲突?

转载 作者:太空狗 更新时间:2023-10-29 13:14:01 24 4
gpt4 key购买 nike

这是我的 git 存储库的初始快照

enter image description here

在分支 master 上,文件 m1 包含

L1

在分支 dev 上,文件 m1 包含

L1
L2

如果我尝试从 master merge dev,它会导致冲突。

$ git checkout master
Switched to branch 'master'

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

$ git diff
diff --cc m1
index 078f94b,9f46047..0000000
--- a/m1
+++ b/m1
@@@ -1,1 -1,2 +1,5 @@@
L1
++<<<<<<< HEAD
++=======
+ L2
++>>>>>>> dev

虽然我没有在master中修改m1的第2行,但是怎么会导致冲突呢?

要验证文件的实际内容并确定这是否是由空格引起的:

在分支 master

git branch
dev
* master

$ xxd m1
0000000: 4c31 0a L1.

在分支 dev

$ git checkout dev
Switched to branch 'dev'

$ xxd m1
0000000: 4c31 0a4c 320a L1.L2.

这是我用来创建这个 repo 的脚本。

#!/bin/bash

mkdir git_demo
cd git_demo
git init

touch m1
git add .
git commit -m "Added file: m1"
# sleep is needed, otherwise a different repo is being created, probably because of *some* filesystem issue!
sleep 1

git branch dev
echo L1 >> m1
git add .
git commit -m "Added line L1 to m1"
# sleep is needed, otherwise a different repo is being created, probably because of *some* filesystem issue!
sleep 1

git checkout dev
echo L1 >> m1
git add .
git commit -m "Added line L1 to m1"
# sleep is needed, otherwise a different repo is being created, probably because of *some* filesystem issue!
sleep 1

echo L2 >> m1
git add .
git commit -m "Added line L2 to m1"
# sleep is needed, otherwise a different repo is being created, probably because of *some* filesystem issue!

gitg --all
git checkout master
git merge dev

最佳答案

答案是存在冲突,因为没有任何merge-base。提交 2 个分支。

以下是如何以更少的步骤生成问题。
创建孤儿分支(孤儿分支是没有任何历史的分支)

enter image description here

你可以在这里看到他们没有共享同一棵树

[ enter image description here ]

enter image description here

enter image description here

关于git - 为什么会导致 merge 冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34416225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com