-2

I know if you modify the same lines in a file and you try to merge the branches, you'd get a merge conflict, since it wouldn't know which version to keep.

So, does this mean if you modify different lines in a file, git would be able to merge them? Does this only apply to addition of new lines or deletions too? What if in one branch, the file is unmodified/untouched and in another, someone deletes a few lines in that file. If we merge the branches, does git keep the deletion?

Thanks!

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
xlalalandx
  • 133
  • 1
  • 5
  • 11
  • Possible duplicate of [How to resolve merge conflicts in Git?](http://stackoverflow.com/questions/161813/how-to-resolve-merge-conflicts-in-git) – George Hilliard Mar 04 '17 at 17:35

2 Answers2

2

Git does not merge files. It merges changes.

Well, technically it merges branches, but if we consider a single file, it merges changes introduced on those branches.

So if, as you say, in one branch we have not modified a file, and in another we have deleted some lines of the file, if you merge, then yes, the deletions will be kept and merged. That is, the final merged files will also have those lines deleted.

The exact algorithm is quite complex and at least more complex than "not the same lines".

For instance, the merge algorithms are in need of identifying the context of a change, and in order to do that it will need surrounding lines that didn't change.

So if in one branch you modify a single line, and in another branch you modify the previous or next line, trying to merge may end up in a merge conflict.

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • I think "merges changes" is more technically-correct than "merges branches", since Git just finds a merge base and compares two commits to that merge base. In particular you can do `git checkout hash1; git merge hash2` to make a detached HEAD commit of a merge of another specific commit. One can argue that these are two anonymous branches, so it's not really clear-cut one way or another, and in any case it's all just fine distinctions, and your answer is right. :-) – torek Mar 05 '17 at 00:22
0

When two users work on the same file, there usually will not be any merge conflicts unless they modify the same lines in the file.

This means that if a line is deleted in one branch and left untouched in another, when the two branches are merged the deletion will be kept.

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225