Resolving Merge Conflicts

By the end of this lesson you'll be able to read Git's conflict markers, resolve a clash by hand, finish the merge with git add and git commit , bail out safely with git merge --abort , and structure your work so conflicts rarely happen at all.

Learn Resolving Merge Conflicts in our free Git course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Git course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

A merge conflict is like two people editing the same sentence of a shared document at the same time. When their edits come together, the document can't show both, so it asks: "Alice wrote blue but Bob wrote green — which should I keep?" Git is exactly that careful editor. It happily combines edits to different sentences on its own, but when two edits land on the same line it stops and lets you , the human, make the final call. A conflict isn't an error or a bug — it's Git refusing to silently throw away someone's work.

1️⃣ Why Conflicts Happen

Git is very good at merging changes automatically. A conflict happens only when two branches modify the exact same lines of the same file . Different files, or different lines of the same file? Git merges them for you with no fuss. Read this worked example, then run a merge and watch Git stop.

2️⃣ Reading the Conflict Markers

When a conflict occurs, Git edits the file and wraps the clashing lines in three markers. Everything between <<<<<<< HEAD and ======= is your version (the branch you're on, called HEAD ); everything between ======= and >>>>>>> is theirs (the branch coming in). Lines outside the markers merged cleanly.

3️⃣ Resolving by Hand

Resolving means making a decision and cleaning up. You edit the file to the version you actually want — keep yours, keep theirs, combine both, or write something new — and then delete all three markers . The conflict is "resolved" the moment no markers remain. Here's the same file from above, finished.

Your turn. Below is a conflict in greeting.txt . You've decided the page should greet returning visitors. Replace the ___ with the one line that should remain — and remember, no markers belong in the finished file.

4️⃣ Finishing the Merge

Editing the file isn't quite the end. You have to tell Git the conflict is fixed by staging the file with git add , then complete the merge with git commit . Use git status at any point to see which files are still "both modified".

Now you finish a merge. You've already edited config.json and removed every marker — only the two final commands remain. Fill in the blanks.

5️⃣ The Escape Hatch: git merge --abort

Sometimes you start a merge and realise it's the wrong branch, or the conflicts are bigger than expected. Don't panic and start deleting markers at random. One command rewinds you to exactly how things were before the merge — like it never happened.

6️⃣ Resolution Tools

You never have to manage markers in a bare text editor. VS Code recognises conflicts and shows clickable Accept Current Change , Accept Incoming Change , and Accept Both Changes buttons right above each clash, plus a side-by-side comparison. Most editors and IDEs (JetBrains, Sublime) have the same. Git also ships git mergetool , which opens a dedicated three-way merge view. Whatever tool you use, the end state is identical: the right content, and no markers left behind.

7️⃣ Preventing Conflicts

The best conflict is one that never happens. Merge from main often so your branch doesn't drift, keep branches small and focused, talk to your team so two people aren't rewriting the same file, and use .gitignore so generated files never collide.

📋 Quick Reference

No blanks this time — just a brief and an outline. Reproduce a real conflict in your own terminal and take it all the way to a clean merge. This is the exact loop you'll run on real projects.

Practice quiz

When does Git raise a merge conflict?

  • When two branches change different files
  • When you create a new branch
  • When a branch only adds new files
  • When two branches change the same lines of the same file

Answer: When two branches change the same lines of the same file. A conflict happens only when two branches modify the exact same lines of the same file, so Git can't auto-merge.

In a conflict, what is the section between <<<<<<< HEAD and =======?

  • The remote's version
  • Your version — the branch you are currently on
  • The version being merged in
  • A deleted file

Answer: Your version — the branch you are currently on. Everything above ======= up to <<<<<<< HEAD is your current branch's version (HEAD).

What does git status show during an unresolved conflict?

  • Files listed as 'both modified' / unmerged paths
  • A clean working tree
  • Nothing at all
  • A list of remotes

Answer: Files listed as 'both modified' / unmerged paths. git status lists conflicted files under 'Unmerged paths' as 'both modified'.

How do you know a conflict is fully resolved in a file?

  • You picked your own side
  • You ran git pull
  • No conflict markers (<<<<<<<, =======, >>>>>>>) remain
  • The file is empty

Answer: No conflict markers (<<<<<<<, =======, >>>>>>>) remain. Git only checks that all three marker lines are gone; it doesn't care which side you kept.

After editing a conflicted file, which command tells Git the conflict is resolved?

  • git resolve
  • git merge --done
  • git fix
  • git add

Answer: git add. Staging the edited file with git add marks that conflict as resolved.

What does git merge --abort do?

  • Rewinds to exactly before the merge, as if it never happened
  • Force-keeps your version everywhere
  • Deletes the conflicted branch
  • Commits the conflict markers

Answer: Rewinds to exactly before the merge, as if it never happened. git merge --abort cancels an in-progress merge and restores the pre-merge state.

After resolving and staging all conflicts, how do you finish the merge?

  • git push
  • git commit
  • git merge --continue only
  • git stash

Answer: git commit. You complete a conflicted merge with git commit; Git pre-fills a merge message for you.

Which command warns you about leftover conflict markers before committing?

  • git status --markers
  • git log --check
  • git diff --check
  • git merge --verify

Answer: git diff --check. git diff --check flags any leftover <<<<<<<, =======, or >>>>>>> marker lines.

Which is a good way to PREVENT conflicts?

  • Merge or pull from main frequently and keep branches small
  • Always work on one giant long-lived branch
  • Never communicate with teammates
  • Commit build output and node_modules

Answer: Merge or pull from main frequently and keep branches small. Small, frequent merges and focused branches mean each merge has far less to clash over.

What does the section between ======= and >>>>>>> branch-name represent?

  • Your current branch's version
  • A backup of the file
  • An empty placeholder
  • Their version — the branch being merged in

Answer: Their version — the branch being merged in. Below ======= up to >>>>>>> branch-name is the incoming branch's version.