Merge Conflicts
Understand and resolve conflicting changes
Skills taught: git.conflicts, git.conflict-resolution
Reading conflict markers
$ git merge feature/login
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
$ git status
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
<nav>
<<<<<<< HEAD
<button class="theme dark">Dark Mode</button>
=======
<button class="theme" data-theme="dark">🌙 Dark</button>
>>>>>>> feature/login
</nav>
<!-- Everything between <<<< HEAD and ==== is YOUR version -->
<!-- Everything between ==== and >>>> is THEIR version -->
Resolving the conflict
# 1. Open the file and edit it
# Keep ours, keep theirs, or combine:
# REMOVE ALL CONFLICT MARKERS (<<<, ===, >>>)
# 2. After editing index.html:
<nav>
<button class="theme" data-theme="dark">🌙 Dark Mode</button>
</nav>
# 3. Stage the resolved file
git add index.html
# 4. Complete the merge
git merge --continue
# (or git commit — Git will pre-fill the merge commit message)
# --- Alternative: take one side wholesale ---
git checkout --ours index.html # keep your version entirely
git checkout --theirs index.html # keep their version entirely
git add index.html
git merge --continue
# --- If you want to give up ---
git merge --abort
Tip: In VS Code, conflicted files show four buttons above each conflict: Accept Current Change, Accept Incoming Change, Accept Both Changes, and Compare Changes. Click the one you want instead of editing markers manually.
Frequently Asked Questions
How do I avoid conflicts?
Pull frequently (git pull at the start of each session). Work in small, focused branches. Communicate with your team when editing shared files. Use git pull --rebase to keep a linear history. Conflicts are a normal part of collaboration — the goal isn't to avoid them entirely, but to resolve them quickly.
package-lock.json always conflicts. Is there a better way?
Yes. Add `package-lock.json merge=npm-merge-driver` to .gitattributes and install npm-merge-driver as a dev dependency. It automatically resolves package-lock.json conflicts using npm's logic.