Lesson Completion
Back to course

Resolving Merge Conflicts

Beginner
10 minutes4.8Git

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:

StepActionCommand
1. EditOpen the file, remove markers, keep correct contentYour editor
2. StageMark the file as resolvedgit add <file>
3. CommitComplete the mergegit 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

flowchart LR CONFLICT["⚠️ Conflicted File"] -->|"1. Edit"| EDITED["✏️ Resolved File"] EDITED -->|"2. git add"| STAGED["📋 Staged"] STAGED -->|"3. git commit"| DONE["✅ Merge Complete"] style CONFLICT fill:#2d1b1b,stroke:#e94560,color:#e94560 style EDITED fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style STAGED fill:#0f3460,stroke:#53d8fb,color:#53d8fb style DONE fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

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

bash
# ─── 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 --abort

Resolution Strategies

StrategyWhen to UseCommand
Manual editMost conflicts — pick the right codeEdit file, git add, git commit
Keep oursYour version is definitely correctgit checkout --ours <file>
Keep theirsTheir version is definitely correctgit checkout --theirs <file>
Visual toolComplex conflicts with many hunksgit mergetool
AbortSomething went wrong, start freshgit merge --abort

Key Takeaways

  • Resolution is 3 steps: edit → stage → commit. No special commands needed.
  • After resolving, git add marks the file as resolved — git commit completes the merge.
  • Use --ours or --theirs for quick whole-file resolutions.
  • Configure a visual mergetool (like VS Code) for complex conflicts.
  • Use git merge --abort if 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 with git add. (3) Complete the merge with git commit.

  • Q: What does git checkout --theirs do 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 to git add and git commit to 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.

Topics Covered

Git BranchingGit Fundamentals

Tags

#git#conflicts#resolve#beginner-friendly

Last Updated

2026-02-12