Pull Requests & Merge Conflicts
20 min
A pull request merge conflict happens when git can't automatically combine two branches' changes to the same file. The key insight: git operates at the LINE-RANGE level, not the whole-file level — two branches can both edit the same file safely, as long as their edited line ranges don't overlap. A conflict only appears when both branches genuinely touched the SAME lines with different content.
def has_conflict(branch_a_changes, branch_b_changes):
for a_start, a_end in branch_a_changes:
for b_start, b_end in branch_b_changes:
if a_start <= b_end and b_start <= a_end:
return True
return False
a_start <= b_end and b_start <= a_end is the standard interval-overlap check -- the exact same test used for calendar scheduling conflicts, resource booking, or any 'do these two ranges intersect' problem, here applied to lines of a file instead of time slots.
Write `has_conflict(branch_a_changes, branch_b_changes)`: each argument is a list of `(start_line, end_line)` ranges that branch modified in the SAME file. Return `True` if any range from `branch_a_changes` overlaps any range from `branch_b_changes`.
Why can two branches BOTH modify the same file without a merge conflict, as long as they touch different line ranges?
You can detect merge conflicts via line-range overlap, the same interval-intersection check git's merge algorithm relies on at its core.