I am new to git and trying to understand merge conflicts. I start with this program on the master branch, in file helloworld.c:
#include <stdio.h>
int main () {
printf("Hello world!\n");
}
I then create a new branch called dev2:
$ git checkout -b dev2
I then edit the file helloworld.c and introduce a bug, to create the file:
#include <stdio.h>
int main () {
printf("Hello world!\n")
}
I then commit the change:
$ git commit -am "Bug"
I then go back to branch master:
$ git checkout master
I would expect that a merge would create conflict. Instead, the merge command just blithely sucks in the bug to my master branch:
$ git merge dev2
Updating 0379d43..b10cde3
Fast-forward
helloworld.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
The bug is now in master branch:
$ 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")
}
It would seem to me that the merging should complain that a line in the file from the dev2 branch is not the same as that line in the same file from the master branch. Why does this merge not create a conflict?
A merge conflict occurs when the branches that are about to get merged modify the same line in different ways. More specifically, it occurs when git doesn't know which version to keep and which to discard. In this case, the change did not conflict with any other change (thanks to @jonrsharpe).
Git also doesn't evaluate any of the code, therefore there is no way for git to know what is a bug and what is not a bug.