为什么引入错误不会与 git 产生合并冲突?

Why doesn't introducing a bug create a merge conflict with git?

我是 git 的新手,正在尝试了解合并冲突。我从主分支上的这个程序开始,在文件 helloworld.c:

#include <stdio.h>

int main () {
  printf("Hello world!\n");
}

然后我创建一个名为 dev2 的新分支:

$ git checkout -b dev2

然后我编辑文件 helloworld.c 并引入一个错误,以创建文件:

#include <stdio.h>

int main () {
  printf("Hello world!\n")
}

然后我提交更改:

$ git commit -am "Bug"

然后我回到分支主管:

$ git checkout master

我预计合并会产生冲突。相反,合并命令只是愉快地将错误吸入我的主分支:

$ git merge dev2
Updating 0379d43..b10cde3
Fast-forward
 helloworld.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

错误现在在 master 分支中:

$ git status
On branch master
Your branch is ahead of 'origin/master' by 17 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

$ cat helloworld.c
#include <stdio.h>

int main () {
  printf("Hello world!\n")
}

在我看来,合并应该抱怨来自 dev2 分支的文件中的一行与来自主分支的同一文件中的那一行不同。为什么这个合并不会产生冲突?

当要合并的分支以不同的方式修改同一行时,就会发生合并冲突。更具体地说,它发生在 git 不知道要保留哪个版本和丢弃哪个版本时。在这种情况下,更改不会与任何其他更改冲突(感谢@jonrsharpe)。

Git 也不评估任何代码,因此 git 无法知道什么是错误,什么不是错误。