tech katta

Resolving merge conflicts

Lesson 16 of 23 · 5 min readFree

In this lesson

Understand why Git stops with a conflict, read the conflict markers, resolve a conflict by editing, and know how to back out safely.

On this page
  1. Why this matters
  2. What a conflict is
  3. Reading the conflict markers
  4. Resolving: edit, add, commit
  5. Backing out: git merge --abort
  6. Taking one whole side
  7. More context with diff3
  8. Other kinds of conflict
  9. Habits that make conflicts smaller
  10. Try it

Sooner or later Git tells you "CONFLICT" and stops in the middle of a merge. It feels like something broke, but nothing is wrong: Git is asking you to make a decision it cannot make on its own. This lesson shows exactly what a conflict is and how to resolve it calmly, step by step.

Why this matters#

On a team, two people regularly change the same file. If you cannot resolve conflicts, you will avoid merging, your branches will drift further from main, and the eventual conflicts will be bigger. Once you have resolved a few, a conflict becomes a five-minute job. And you should know the way out: you can always abort and return to where you started.

The repository used here is a small API with one file, config.js, which holds the port, log level and database address. All outputs are real.

What a conflict is#

Lesson 15 showed that Git merges automatically when two branches change different parts of the code. A conflict happens when both branches changed the same lines in different ways, or one branch changed a file that the other deleted. Git cannot know which version you want, so it stops, marks the problem and waits.

The setup: config.js starts with port 5100 and log level info. On a feature branch, someone changed the port to 8080 and the log level to debug. Meanwhile, on main, someone else changed the port to 5200. Merging the feature branch into main:

Shell
git merge feature/port
Text
Auto-merging config.js
CONFLICT (content): Merge conflict in config.js
Automatic merge failed; fix conflicts and then commit the result.

The exit code of the command is 1, so a script would see this as a failure too. You are now in the middle of a merge. Do not panic, and do not run random commands. First, ask Git what is going on:

Shell
git status
Text
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:   config.js

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

Git names the file with the problem and tells you your two options: fix it and commit, or abort. git status -s shows the short code UU for "unmerged, modified on both sides".

Reading the conflict markers#

Git wrote the competing versions into the file itself, between three kinds of marker lines:

Text
module.exports = {
<<<<<<< HEAD
  port: 5200,
  logLevel: "info",
=======
  port: 8080,
  logLevel: "debug",
>>>>>>> feature/port
  db: "postgres://localhost/app",
};
  • Everything between <<<<<<< HEAD and ======= is your side: the branch you are on (main).
  • Everything between ======= and >>>>>>> feature/port is their side: the branch you are merging in.
  • Lines outside the markers, like the db line, merged without any problem.

Notice that the log level conflicts only because it sits right next to the port line; Git compares in chunks of adjacent lines. The file as it stands is not valid JavaScript. If you commit it, the application will crash on startup.

Resolving: edit, add, commit#

Resolving a conflict is nothing more than editing the file until it contains what you want, then telling Git you are done. Here we decide to keep port 5200 from main and the debug log level from the feature branch. Edit the file to look like this, with every marker line removed:

Text
module.exports = {
  port: 5200,
  logLevel: "debug",
  db: "postgres://localhost/app",
};

Check that no markers remain, mark the file as resolved with git add, and commit:

Shell
grep -n '<<<<<<<\|>>>>>>>\|=======' config.js || echo "no markers left"
git add config.js
git status -s
git commit -m "Merge feature/port"
git log --oneline --graph
Text
no markers left
M  config.js
[main ea8287a] Merge feature/port
*   ea8287a Merge feature/port
|\  
| * 6c73382 Use port 8080 and debug logging
* | 6dd46b5 Move API to port 5200
|/  
* 094164d Add config

git add here does not mean "add a new file". It means "this file is resolved, put this version in the staging area". The commit that follows is the merge commit with two parents, just like the automatic one in lesson 15. If you want, you can run git commit with no message and accept the default merge message that Git proposes.

The important thing is that you decide what the result should be. It may be one side, the other side, or a mix like ours. Often the right answer needs a quick conversation with whoever wrote the other change. After resolving, run the application or its tests. A file that merges cleanly can still be wrong, and one that you resolved by hand even more so.

Many editors, including VS Code, show conflicts with buttons such as "Accept current change" and "Accept incoming change". They do the same text editing for you. You can always do it by hand.

Backing out: git merge --abort#

If you are not sure, or you started the wrong merge, abort. The repository goes back exactly to the state before you ran git merge:

Shell
git merge --abort
git status -s
git log --oneline | head -1
Text
clean after abort: 0
bed0b47 db2

We counted the lines of git status --porcelain and found zero, so the working tree was clean, and the last commit was the same one we started with. Nothing was lost. If you made edits to the conflicted files before aborting, those edits are discarded, so abort early rather than late.

Taking one whole side#

When you know that one side is simply right for a file, you can take it entirely instead of editing:

Shell
git checkout --theirs config.js
git add config.js
git commit -m "Merge b1 (take theirs)"

--theirs is the version from the branch being merged in, and --ours is your current branch. Be careful: this discards the other side for that whole file, not just for the conflicting lines. Newer Git versions also offer git restore --theirs config.js for the same purpose.

More context with diff3#

By default Git shows only the two sides. Sometimes it helps to see the original too. Ask for the diff3 style:

Shell
git -c merge.conflictStyle=diff3 merge b2
Text
<<<<<<< HEAD
  logLevel: "error",
||||||| 6d0cc4b
  logLevel: "debug",
=======
  logLevel: "warn",
>>>>>>> b2

The middle part, after |||||||, is the common ancestor: what the line looked like before either side changed it. Seeing that debug was changed to error on one side and to warn on the other makes the intent of each change clear. You can make this the default with git config --global merge.conflictStyle diff3.

Other kinds of conflict#

A conflict is not always about lines of text. If one branch deletes a file and the other edits it, you get a modify/delete conflict:

Text
CONFLICT (modify/delete): config.js deleted in b3 and modified in HEAD.  Version HEAD of config.js left in tree.

git status -s shows UD (deleted by them). You decide: keep the file with git add config.js, or accept the deletion with git rm config.js. Either way you then commit.

Habits that make conflicts smaller#

  • Keep branches short-lived and merge often, so the two sides do not drift apart.
  • Pull the latest main into your branch regularly, so you resolve small conflicts early.
  • Make small commits and avoid reformatting whole files in the same commit as a real change.
  • Tell teammates when you are about to rewrite a big shared file.
  • If you resolve a conflict in several files, resolve them one at a time and run git status between them.

To catch a marker that you forgot to remove, run git diff --check. It reports leftover conflict markers, and it also finds trailing whitespace:

Text
f.txt:1: leftover conflict marker
f.txt:3: leftover conflict marker
f.txt:5: leftover conflict marker

Try it#

  1. Create a repository with a file config.txt with three lines. Branch off, change line 2 on the branch and commit. Switch to main, change line 2 differently, and commit. Merge the branch and read git status.
  2. Open the file, read the markers, and find which side is HEAD. Edit the file to keep a combination of both changes. Run grep to check that no markers remain.
  3. Run git add and git commit, and look at git log --oneline --graph.
  4. Repeat the setup and this time abort the merge with git merge --abort. Confirm with git status that you are back to normal.
  5. Create the same conflict again and use git -c merge.conflictStyle=diff3 merge to see the common ancestor section.