对于测试文件,我做了一些更改并提交了它们。 为了让大家都能跟上这些变化,这里是 git log -p 的输出:
commit 6d1ab83bd1b9fbe0b0953eeb209afec90f592b7d (HEAD -> main)
a further line
diff --git a/test.txt b/test.txt
index 987fb68..9692e29 100644
--- a/test.txt
+++ b/test.txt
@@ -4,4 +4,6 @@ here i am
i have never been there
+a further line
+
commit 35f743c6ae1835182691729eaf447b09337d2ed6
hello -> hio and newline added
diff --git a/test.txt b/test.txt
index c511e9f..987fb68 100644
--- a/test.txt
+++ b/test.txt
@@ -1,5 +1,7 @@
-hello
+hio
here i am
+i have never been there
+
然后我调用了 git revert 35f743c 以撤消之前的一些更改。这导致了 merge 冲突。在编辑器中查看文件后,我得到以下输出:
hello
here i am
<<<<<<< HEAD
i have never been there
a further line
=======
>>>>>>> parent of 35f743c (hello -> hio and newline added)
现在我不明白的是: 从“hello”到“hio”的改变不被指示为冲突,而行“i never be there”被指示为冲突。为什么会出现这种情况?我的意思是:35f74 提交将第一行更改为“hio”,并添加了“我从未去过那里”这一行。随后的提交没有触及任何这些行,当我想撤消(恢复)这两个更改时,第一个更改被愉快地接受,而第二个更改则导致 merge 冲突。
我想如果有人能够解释 git 中文件比较的底层机制将会有所帮助。
最佳答案
在恢复提交时35f743c
git 期望行为
i have never been there
但该声明大致如下:
i have never been there\n
a further line
我想这只是因为相邻线而成为问题。这个想法是,相邻的行为更改提供上下文,从而为提交提供上下文。如果你在最后保留一个空行,这将不会显示。
这里有更多reading
关于git - 了解 git 恢复冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77279564/