The Hook (The "Byte-Sized" Intro)
Conflict markers look terrifying the first time. By the tenth time, they're a 30-second task. The secret: there are exactly 3 steps — edit, stage, commit. That's it. No special commands, no wizardry. Open the file, pick the right code, delete the markers, and move on. Every senior developer resolves conflicts on autopilot.
📖 What is Resolving Merge Conflicts?
Resolving a conflict means opening the conflicted file, choosing the correct content, removing the conflict markers, then staging and committing to complete the merge. You can also use merge tools for a visual side-by-side experience.
Conceptual Clarity
The 3-step resolution process:
| Step | Action | Command |
|---|---|---|
| 1. Edit | Open the file, remove markers, keep correct content | Your editor |
| 2. Stage | Mark the file as resolved | git add <file> |
| 3. Commit | Complete the merge | git commit (message auto-generated) |
Resolution options:
- Keep ours: Keep only your branch's changes
- Keep theirs: Keep only the merging branch's changes
- Keep both: Keep both changes (in the right order)
- Write new: Combine or rewrite both versions into something better
Real-Life Analogy
Resolving a conflict is like being the final editor of a document where two writers changed the same paragraph. You read both versions, decide on the final wording, and submit the approved draft. The document then moves forward with everyone on the same page.
Visual Architecture
Why It Matters
- Correctness: A badly resolved conflict introduces bugs that are hard to trace.
- Speed: Knowing the 3-step process means conflicts never block you for more than a minute.
- Confidence: Teams that handle conflicts smoothly ship faster and with less stress.
- Tools exist: VS Code, IntelliJ, and other editors offer visual merge tools that make resolution even easier.
Code
# ─── After a merge conflict occurs ───
git status
# Output:
# Unmerged paths:
# both modified: config.txt
# ─── Step 1: Open and edit the file ───
# Before editing:
cat config.txt
# <<<<<<< HEAD
# color = blue
# =======
# color = green
# >>>>>>> feature/theme
# After editing (you chose "green" or combined both):
echo "color = green" > config.txt
# ─── Step 2: Stage the resolved file ───
git add config.txt
# ─── Step 3: Complete the merge ───
git commit
# Git auto-generates a merge commit message:
# "Merge branch 'feature/theme' into main"
# ─── Alternative: Use a merge tool ───
git mergetool
# Opens your configured visual merge tool (VS Code, vimdiff, etc.)
# ─── Set VS Code as your merge tool ───
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait --merge $REMOTE $LOCAL $BASE $MERGED'
# ─── Resolve using ours or theirs shortcuts ───
# Keep our version (current branch) for entire file:
git checkout --ours config.txt
git add config.txt
# Keep their version (merging branch) for entire file:
git checkout --theirs config.txt
git add config.txt
# ─── Abort the entire merge (start over) ───
git merge --abortResolution Strategies
| Strategy | When to Use | Command |
|---|---|---|
| Manual edit | Most conflicts — pick the right code | Edit file, git add, git commit |
| Keep ours | Your version is definitely correct | git checkout --ours <file> |
| Keep theirs | Their version is definitely correct | git checkout --theirs <file> |
| Visual tool | Complex conflicts with many hunks | git mergetool |
| Abort | Something went wrong, start fresh | git merge --abort |
Key Takeaways
- Resolution is 3 steps: edit → stage → commit. No special commands needed.
- After resolving,
git addmarks the file as resolved —git commitcompletes the merge. - Use
--oursor--theirsfor quick whole-file resolutions. - Configure a visual
mergetool(like VS Code) for complex conflicts. - Use
git merge --abortif you need to start the merge over.
Interview Prep
-
Q: What are the steps to resolve a merge conflict? A: (1) Open the conflicted file and remove the conflict markers (
<<<<<<<,=======,>>>>>>>), keeping the correct content. (2) Stage the file withgit add. (3) Complete the merge withgit commit. -
Q: What does
git checkout --theirsdo during a conflict? A: It replaces the conflicted file with the version from the merging branch, discarding your branch's changes for that file. You still need togit addandgit committo complete the merge. -
Q: How do you abort a merge that has conflicts? A: Run
git merge --abort. This resets the working tree and staging area to the state before the merge started. No changes from the merge are kept.