Merge Conflict
Situation in version control where changes from different branches touch the same part of a file and cannot be combined automatically.
A merge conflict arises when a version control system tries to combine two lines of development and finds changes it cannot reconcile on its own. Most merges succeed automatically: if one developer edits the top of a file and another edits the bottom, both changes can simply be kept. A conflict occurs when both branches modify the same lines differently, or when one branch edits a file that the other deleted. The system then pauses the merge and asks a person to decide what the combined result should be.
Tools such as Git use a three-way merge, comparing each branch against their most recent common ancestor to see what each side changed. When the changes overlap, Git writes both versions into the file between conflict markers, lines of less-than, equals and greater-than signs, and marks the file as unmerged. The developer edits the file to the intended result, which may combine both changes, keep one or require writing something different, removes the markers and commits the resolution. Graphical merge tools and editors such as Visual Studio Code present the competing versions side by side.
Conflicts are a normal part of collaboration rather than an error, but they cost time and can introduce bugs when resolved hastily, since code that merges cleanly is not necessarily correct. Teams reduce them by merging small changes frequently, a core idea of continuous integration, by keeping pull requests short-lived and by agreeing on automatic formatting so that style changes do not collide. Automated tests after a merge catch problems that textual merging misses, such as two changes that are each valid on their own but break each other when combined.
Key points
- A merge conflict occurs when two branches change the same lines or one edits a file the other deleted.
- Git compares both branches with their common ancestor and marks unresolvable overlaps with conflict markers.
- Resolving a conflict requires a person to decide the correct combined result and commit it.
- Frequent small merges, short-lived branches and automated tests reduce both conflicts and merge-related bugs.
Related entries
Version Control
System that records changes to files over time so that people can track history, revert mistakes and collaborate on the same codebase.
Git
Free and open-source distributed version control system created by Linus Torvalds in 2005, used to track changes in source code.
Pull Request
Proposal to merge a set of code changes from one branch or fork into another, used as the focal point for review and discussion.
Continuous Integration
Development practice in which programmers merge changes into a shared repository frequently, with each change verified by an automated build and tests.
Code Review
Practice in which developers examine each other's source code changes to find defects, share knowledge and keep the codebase consistent.
GitHub
Web platform owned by Microsoft for hosting Git repositories and collaborating on software through pull requests, issues and automation.
Visual Studio Code
Free source-code editor from Microsoft, built on open-source foundations, with a large extension ecosystem and built-in Git and debugging support.