Imagine you're working on a feature branch, and at the same time, a colleague is working on a different feature (or even a bug fix) on another branch. Eventually, you'll want to combine these changes, often into a main or develop branch. This process is called merging. A "merge conflict" occurs when Git can't automatically figure out how to combine changes from two different branches. This typically happens when both branches have modified the same lines in the same file, or if one branch deleted a file that the other modified. Git is smart, but it can't read minds to know which change is the "correct" one, so it asks you to decide.
When a merge conflict happens, Git will pause the merge and tell you which files have conflicts. If you open a conflicted file, you'll see special markers that Git inserts to highlight the problem areas. <<<<<<< HEAD indicates the changes from your current branch (often HEAD refers to the branch you are currently on and trying to merge into). ======= acts as a separator between the conflicting changes. >>>>>>> branch-name indicates the changes from the branch you are trying to merge from. Your task is to manually edit the file, deciding which version of the code to keep – your changes, their changes, or a combination of both – and then remove these markers.
Resolving a conflict is a hands-on process. First, identify the conflicted files (using git status). Then, open each file in your code editor. Carefully examine the code between the <<<<<<<, =======, and >>>>>>> markers. You'll need to choose which code block to keep, or modify it to incorporate elements from both, ensuring the final code is correct and functional. Once you've made your decision, delete all the conflict markers (the <<<<<<<, =======, >>>>>>> lines). After saving the modified file, you must tell Git that the conflict is resolved by running git add <conflicted-file>. Repeat for all conflicted files. Finally, complete the merge by running git commit -m "Resolve merge conflict". This creates a new commit that incorporates all the merged changes, including your conflict resolution.
Key Takeaways
- Merge conflicts occur when Git can't automatically combine changes, usually affecting the same lines in the same file.
- Git uses
<<<<<<< HEAD,=======, and>>>>>>> branch-namemarkers to highlight conflicting sections in files. - To resolve, manually edit the file, choose the desired code, and remove all conflict markers.
- After editing,
git add <file>the resolved files and thengit committo complete the merge. - Resolving conflicts is a normal, everyday part of collaborative software development.
Code Example
How this code works
This code example illustrates a JavaScript file, app.js, caught in a Git merge conflict. Its job is to show precisely how Git marks the points of disagreement when two branches, in this case, the current branch (HEAD) and a feature branch (feature/new-greeting), have both made different changes to the same lines of code within the greet function. The file is presented in the state it would be in immediately after Git tried to merge and failed automatically, leaving the conflict for manual resolution.
Git uses special markers to highlight the conflicting sections. The lines between <<<<<<< HEAD and ======= represent the version of the greet function from the branch that was checked out (the current branch). Below the ======= marker, the lines up to >>>>>>> feature/new-greeting show the alternative version of the greet function from the branch being merged into the current one. A subtle but crucial point for beginners is that these <<<<<<<, =======, and >>>>>>> markers are not valid JavaScript syntax. If this file were run directly, it would result in a syntax error; they are solely Git's way of pointing out what needs to be fixed. The greet('World') call remains unaffected as it was not part of the conflicting changes.