Reputation: 9143
look at this sequence:
First, i am cloning an empty repository:
git clone http://mygitlabserver/myuser/project1.git
cd project1
Then i create a main.c file with this content:
#include <stdio.h>
int main(int argc, char *argv[]) {
return 0;
}
I add and commit this very first version of my program:
git add main.c
git commit -m "first version"
Then, i create a branch and i switch to this branch:
git branch branch1
git checkout branch1
In this branch, i made a change to main.c file:
#include <stdio.h>
int main(int argc, char *argv[]) {
return 1;
}
As you can see, this is a very basic change (0 to 1).
I add and commit this change on branch1:
git add main.c
git commit -m "version with return 1"
Now, i want to merge branch1 to master branch. Here is what i type:
git checkout master
git merge branch1
I have no warning about conflict and this is what i do not understand. Here is content of main.c file now in master branch:
#include <stdio.h>
int main(int argc, char *argv[]) {
return 1;
}
So my main.c file was replaced by the one in branch1. Sometimes, i see my main.c file with something like this:
#include <stdio.h>
int main(int argc, char *argv[]) {
==============================
return 0;
==============================
return 1;
==============================
}
But this was not the case in my example. My question is... Why?
Thanks
Upvotes: 1
Views: 33
Reputation: 1724
There is no conflict. Both branches have same history with last commit "first version" and this commit is the last one on main
branch, which makes the merging trivial. Merging branch1
to master
applies commit "version with return 1" the same way you have applied it when you created it on branch1
.
If you have created for e.g. a commit "version with return 2" on main
branch (which obviously changes return 0
to return 2
), then histories would have diverged AND differences would apply to the same part of file. This would result in conflict.
Upvotes: 1