tech
Chapter 4: Merge Conflicts & Resolution
Learn what merge conflicts mean, why they happen, how to read conflict markers, and how to resolve conflicts with confidence.
Chapter 4: Merge Conflicts & Resolution
Merge conflicts are the leading cause of developer anxiety when using Git. However, a merge conflict is not a system crash or an error. It is simply Git asking a human to make a coding decision.
🍽️ The Real-World Analogy: The Restaurant Menu
Imagine you and a partner co-own a restaurant. You have a shared menu document printed in your kitchen. On line 5, the menu says:
Burger: $10
The Conflict:
- You take the menu home, open it, and change line 5 to
Burger: $12because meat prices rose. - Your partner also takes a copy home and changes line 5 to
Burger: $15to include gourmet fries.
The next day, you both go to the printing shop (Git) to merge your updates into a single new menu.
The printer looks at line 5:
- One owner says it should be
$12. - The other owner says it should be
$15.
The printer cannot print the menu because it doesn’t know which price is correct. It stops and highlights line 5, asking both of you to sit down, discuss, and write the final price.
This is a merge conflict.
💥 How Conflicts Occur in Git
Git is excellent at merging files automatically. If Developer A edits the top of a file, and Developer B edits the bottom of the same file, Git will automatically merge their changes when they pull or merge.
A conflict only occurs when:
- Two different commits modify the exact same line(s) of a file.
- One commit deletes a file that another commit is trying to modify.
Git stops the merge, enters a “Merging” state, and leaves it up to you to clean up the affected files.
🔍 Reading Git Conflict Markers
When a conflict occurs, Git modifies the conflicted code files on your hard drive. If you open a conflicted file, you will find these markers:
<<<<<<< HEAD
const databaseUrl = "mongodb://localhost:27017/local_db";
=======
const databaseUrl = "mongodb://cloud-server.com:27017/prod_db";
>>>>>>> feature-production-db
To resolve the conflict, you must parse these three parts:
<<<<<<< HEADThis is the start of the conflict. The code directly below this marker is what exists on the branch you are currently sitting on (your active branch).=======This is the division line. Everything above it belongs to your current branch; everything below it belongs to the incoming branch.>>>>>>> feature-production-dbThis is the end marker. The code directly above it is what exists on the branch you are trying to merge in (in this case,feature-production-db).
🛠️ The Step-by-Step Conflict Resolution Workflow
When a merge stops with a conflict, follow this exact workflow. Do not panic.
Step 1: Find the Conflicted Files
Run git status to see exactly which files are blocking the merge.
git status
Look for files listed under “Unmerged paths” (often highlighted in red). They will say both modified: filename.js.
Step 2: Open and Resolve the Files
Open the conflicted files in a code editor like Visual Studio Code. VS Code has built-in UI buttons to help you resolve them:
- Accept Current Change: Keeps the code between
HEADand===(your current branch). - Accept Incoming Change: Keeps the code between
===and>>>>>>>(the other branch). - Accept Both Changes: Keeps both blocks of code.
If you don’t use these buttons, you can simply delete the markers manually and type the final version of the code:
// Manually edited to use the cloud database URL
const databaseUrl = "mongodb://cloud-server.com:27017/prod_db";
⚠️ Important: You must delete the <<<<<<<, =======, and >>>>>>> lines completely.
Step 3: Stage the Resolved Files
Once your file is edited and saved, tell Git you have resolved the conflict by staging it:
git add databaseUrl.js
Step 4: Finalize the Merge Commit
After staging all conflicted files, check git status again to make sure everything is green. Then, complete the merge:
git commit -m "Merge and resolve database URL conflict"
🛟 The Emergency Escape Hatch
If you get stuck, confused, or realize you merged the wrong branch, you can abort the operation and return everything to the way it looked before you typed the merge command:
# Aborts a merge process and restores your working directory
git merge --abort
If you are rebasing and run into a conflict:
# Aborts a rebase process
git rebase --abort
💡 Summary of Chapter 4
- Merge conflicts are not errors; they are decisions Git cannot make on its own.
- Markers (
<<<<<<<,=======,>>>>>>>) isolate the conflicting code blocks. - To resolve, edit the file to your desired code state, delete the markers,
git addthe file, andgit committo seal the resolution. - Use
git merge --abortif you need to start over safely.
Now, let’s head to Chapter 5 to practice these concepts in your terminal.