I've been using git for quite a while but I never really understood why merge conflicts happen. I learnt to resolve them quickly but I feel like not understanding why they happen makes me unhappy.
The most common explanation is the same code region was changed by 2 different commits and now git cannot decide which one to pick
makes sense only for the simplest cases.
In order to understand better what's going on I created a simple file with 2 lines:
a
b
and commited it (log message init, branch master).
Then I created a feat
branch from master
and added a 3rd line: c
Then I created another feat2
branch from master
and changed b
to bb
.
Finally, I changed the first line in master
to ax
.
The resulting tree looks like this:
$ git tree
* f4c4a9d (feat) add 3rd line: c
| * f050bf8 (feat2) changed 2nd line: b -> bb
|/
| * 244ed21 (HEAD -> master) change 1st line: a -> ax
|/
* 51fca0f init
If I want to cherry-pick or merge feat
into master it works fine but if I want to cherry-pick feat2
which changes the 2nd line it fails and can't figure out why that happens.