TetSharing
Git & CLI tutorial · 12 chapters
  1. Terminal basics
  2. Working with files
  3. Pipes and redirection
  4. What Git is
  5. Staging and commits
  6. Viewing history
  7. Comparing changes
  8. Branches
  9. Merging
  10. Merge conflicts
  11. Undoing changes
  12. Remotes, push and pull

Final Git & CLI quiz
Cheat sheet: git commands
Cheat sheet: linux shell commands

Chapter 10 of 12 · By Ethan Cole · Updated November 21, 2026

Resolve Git Merge Conflicts Step by Step

A merge conflict happens when two branches changed the same lines of the same file in different ways. Git cannot know which version is right, so it stops and asks you. Conflicts are normal on any shared project and they are not dangerous: nothing is lost, and you can always back out.

What Git does

When a merge hits a conflict, Git:

  1. Merges every file it can automatically.
  2. Prints CONFLICT (content): Merge conflict in <file> for each problem file.
  3. Writes both versions into those files, between conflict markers.
  4. Leaves the merge unfinished, waiting for you.

git status now lists the files under "Unmerged paths" as "both modified".

Reading the markers

<<<<<<< HEAD
The meeting is on Tuesday.
=======
The meeting is on Thursday.
>>>>>>> reschedule

Everything between <<<<<<< HEAD and ======= is your side, the branch you are on. Everything between ======= and >>>>>>> reschedule is the side being merged in. Lines outside the markers merged cleanly.

Resolving

  1. Open the file and decide what the result should be: one side, the other, or a combination.
  2. Delete all three marker lines and leave the final text.
  3. git add the file to mark it as resolved.
  4. git commit to finish the merge (Git prepares a message for you).

In the transcript, both branches edit the same line. The merge stops with a conflict, cat shows the markers, and then we write the agreed text, stage it and commit. Editors such as VS Code highlight conflict blocks and offer "accept current", "accept incoming" or "accept both" buttons that do step 2 for you.

Terminal · bash + git · copy and run locally
git init -q
echo "The meeting is on Monday." > plan.txt
git add plan.txt
git commit -q -m "Plan meeting"
git switch -q -c reschedule
echo "The meeting is on Thursday." > plan.txt
git commit -q -am "Move to Thursday"
git switch -q main
echo "The meeting is on Tuesday." > plan.txt
git commit -q -am "Move to Tuesday"
git merge reschedule
git status --short
cat plan.txt
echo "The meeting is on Thursday at 10am." > plan.txt
git add plan.txt
git commit -q --no-edit
git log --oneline --format="%s"

Transcript (verified by our test suite in a fresh temp folder)

$ git init -q
$ echo "The meeting is on Monday." > plan.txt
$ git add plan.txt
$ git commit -q -m "Plan meeting"
$ git switch -q -c reschedule
$ echo "The meeting is on Thursday." > plan.txt
$ git commit -q -am "Move to Thursday"
$ git switch -q main
$ echo "The meeting is on Tuesday." > plan.txt
$ git commit -q -am "Move to Tuesday"
$ git merge reschedule
Auto-merging plan.txt
CONFLICT (content): Merge conflict in plan.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git status --short
UU plan.txt
$ cat plan.txt
<<<<<<< HEAD
The meeting is on Tuesday.
=======
The meeting is on Thursday.
>>>>>>> reschedule
$ echo "The meeting is on Thursday at 10am." > plan.txt
$ git add plan.txt
$ git commit -q --no-edit
$ git log --oneline --format="%s"
Merge branch 'reschedule'
Move to Tuesday
Move to Thursday
Plan meeting

Backing out

If a conflict is bigger than you expected, git merge --abort puts everything back the way it was before you started the merge. Nothing from either branch is lost. You can then talk to the person who wrote the other change, or merge main into your branch in smaller steps.

Making conflicts rarer

  • Merge or pull from main often, so branches do not drift far apart.
  • Keep commits and branches small and focused.
  • Avoid reformatting whole files in the same branch as real changes; formatting touches every line and collides with everyone.

Common mistakes

  • Committing a file that still contains <<<<<<< markers. Search for them before git add.
  • Picking one side blindly. Read both: often the right answer combines them.

Summary

  • Conflicts mean both sides changed the same lines; Git pauses the merge.
  • Edit the file, remove the markers, git add, then git commit.
  • git merge --abort safely cancels.

Quiz: Merge conflicts

1. Between which markers is the version from your current branch?

The HEAD side is above the ======= line.

2. After editing a conflicted file into its final form, what marks it as resolved?

Staging the file tells Git the conflict is resolved; then commit.

3. How do you cancel a merge that stopped with conflicts?

--abort restores the state from before the merge started.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.