Skip to content
Laviyx Learn

Try

Git and GitHub course menu

How to Resolve Merge Conflicts in Git, Step by Step

Understand why Git merge conflicts happen, read conflict markers, fix a conflict by hand, finish or abort the merge, handle deleted files, and avoid conflicts in a team.

Beginner7 min readUpdated
On this page

A merge conflict happens when two branches change the same lines of a file, and Git cannot know which version you want. Conflicts are normal in team work. They are not errors, and nothing is lost: Git stops and asks you to decide.

Create a conflict#

Two branches change the opening hours in different ways:

Terminal
git switch -q -c update-hours
sed -i 's/Open 9 to 5/Open 8 to 6/' about.txt
git commit -q -am "Extend opening hours"
git switch -q main
sed -i 's/Open 9 to 5/Open 10 to 4/' about.txt
git commit -q -am "Shorten opening hours"

Now merge:

Terminal
git merge update-hours
Output
Auto-merging about.txt
CONFLICT (content): Merge conflict in about.txt
Automatic merge failed; fix conflicts and then commit the result.

The merge has paused. git status tells you what is going on:

Terminal
git status
Output
On branch main
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:   about.txt

no changes added to commit (use "git add" and/or "git commit -a")

Read the conflict markers#

Git has written both versions into the file:

Terminal
cat about.txt
Output
Laviyx Shop
<<<<<<< HEAD
Open 10 to 4
=======
Open 8 to 6
>>>>>>> update-hours
Free delivery
MarkerMeaning
<<<<<<< HEADStart of your version (the current branch)
=======Divider between the two versions
>>>>>>> update-hoursEnd of the other branch's version

The lines outside the markers merged normally.

Fix it#

  1. Open the file and edit it so it says what you want. That may be one version, the other, or a mix of both.
  2. Delete all three marker lines.
  3. Stage the file.
  4. Finish the merge.

Here we decide on new hours. We write the final content, which is the same as editing the file by hand:

Terminal
printf 'Laviyx Shop\nOpen 8 to 5\nFree delivery\n' > about.txt
git add about.txt
git commit --no-edit
git log --oneline --graph
Output
[main b9abb3f] Merge branch 'update-hours'
*   b9abb3f Merge branch 'update-hours'
|\  
| * 62a6604 Extend opening hours
* | 62c7547 Shorten opening hours
|/  
* 7152b7e Add about page

git add is how you tell Git "this conflict is resolved". git commit then creates the merge commit.

Changed your mind?#

Before committing, you can cancel the merge and return to where you started:

Terminal
git merge --abort

Choosing a whole side#

If you want one branch's version of a whole file, there is a shortcut during a merge:

Terminal
git checkout --ours about.txt     # keep the current branch's version
git checkout --theirs about.txt   # keep the other branch's version
git add about.txt

Use these with care: they replace the whole file, including any changes that merged cleanly.

Conflicts with deleted files#

If one branch edits a file while another deletes it, git status shows deleted by us or deleted by them. Decide what you want, then run git add file to keep it or git rm file to delete it.

Using an editor#

VS Code highlights conflicts and shows Accept Current, Accept Incoming and Accept Both buttons above each one. These just edit the text for you, so check the result and run the code before committing.

Avoiding conflicts#

  • Pull from main often, so your branch does not drift far away.
  • Keep branches small and short-lived.
  • Commit formatting changes separately from real changes.
  • Talk to teammates before large renames or moves.

Common mistakes#

Practice#

Practice exercise

The branches main and green both changed theme.txt. Merge green into main, resolve the conflict by choosing Colour: green, and finish the merge. Then print the file and the last commit's message.

Show solution
Terminal
git merge green > /dev/null
echo "Colour: green" > theme.txt
git add theme.txt
git commit -q --no-edit
cat theme.txt
git log -n 1 --format=%s
Output
Colour: green
Merge branch 'green'

Key takeaway#

Next: Saving Work with git stash.

Finished this lesson?