Skip to content

GeneralGit from scratch

5. Conflicts

Guide · git, version control · updated on 11/08/2026

Author: Ángel Fernández Caravaca
Date: August 11, 2026

Chapter 4 ended by introducing conflicts during a merge operation. That is where this chapter picks up, since a conflict is the moment Git stops and hands the decision back to you. In this chapter we will learn why two branches end up disagreeing, how to read the markers Git writes into your files, how to resolve a conflict without losing either side of the work and how to get out of one if you'd rather not deal with it right now.

Why a conflict happens

Remember the three points Git compares during a merge: the merge base, the tip of the branch you are on and the tip of the branch you are merging in. Git looks at the base and asks each side the same question, what have you changed since we diverged?, and then tries to apply both answers to the same file.

As long as the two answers touch different regions, applying both is unambiguous and Git does it without a word. A conflict is what Git reports when the two answers overlap, that is, when both sides rewrote the same region of the same file and each claims different content for it. There is no rule that can pick a winner, since preferring the most recent change or the longest one would be guesses about your intention.

So it is worth making clear what a conflict is not:

  • It is not an error. Nothing has gone wrong, nothing is broken and no command was typed incorrectly.
  • It is not data loss. Both versions are stored safely in objects/, exactly as Chapter 1 described, and both are still reachable from their respective branches.
  • It is not a state you can't get out of. As we will see, a single command undoes everything.

A conflict is simply a question, that is, Git telling you that those specific lines have two candidate contents and that only you know which one the project should keep.

Note

The unit of comparison is the line, not the "idea". Since Git compares text line by line, two people editing the same line produce a conflict even if their edits are trivially compatible, whereas two people editing neighbouring lines usually produce none, even if their edits are logically incompatible. Git guarantees that the text is integrated, never that the result makes sense, which is why the resulting file still has to be read and tested.

Not just content: the other kinds of conflict

The conflict everyone pictures is two versions of the same lines, and it is by far the most common, but it is not the only way two branches can disagree. Git states the kind in the message it prints, and reading that word saves you from hunting for markers in a file that has none.

Kind What each side did What Git prints
Content Both rewrote the same region of the same file CONFLICT (content): Merge conflict in file.txt
Modify/delete One side edited the file while the other deleted it CONFLICT (modify/delete): file.txt deleted in feature and modified in HEAD
Add/add Both created a file with the same name and different content CONFLICT (add/add): Merge conflict in file.txt
Rename/rename Both renamed the same file, each to a different name CONFLICT (rename/rename): file.txt renamed to a.txt in HEAD and to b.txt in feature
Directory/file One side turned a path into a directory while the other kept it as a file CONFLICT (file/directory): ...

Only the first and third leave markers inside the file, since only there does Git have two texts to interleave. The rest are conflicts about the path itself, so resolving them is not about editing lines but about deciding whether the file exists and under what name. For example: in a modify/delete conflict the file stays on disk with the modified content, and resolving it means running git add file.txt if you want to keep it, or git rm file.txt if you agree with the deletion.

Anatomy of the markers

When the conflict is about content, Git does not leave the file untouched waiting for instructions. It writes into your working directory a version of the file that contains everything it could integrate on its own, plus the two candidates for the region it couldn't, delimited by markers:

1
2
3
4
5
6
7
8
9
Common text both branches agree on, integrated normally.

<<<<<<< HEAD
the content as it is on the branch you are on
=======
the content as it is on the branch you are merging in
>>>>>>> feature

More common text, again integrated normally.

Three markers appear, and each one is a boundary, not content:

  1. <<<<<<< HEAD opens the conflicting region, and what follows it is your side, that is, the version reachable from the branch HEAD is attached to.
  2. ======= separates the two candidates, so it is a divider and not part of either version.
  3. >>>>>>> feature closes the region, and the name after it tells you where the second block came from, that is, the branch you passed to git merge.

Notice that the rest of the file is perfectly normal text, since Git only marks the regions it couldn't decide. A file with three conflicting regions has three of these blocks, while the hundreds of lines around them are already integrated and need nothing from you.

Important

Those seven-character markers are normal text inside your file. Git doesn't track them, doesn't know whether you've removed them and will happily commit them if you leave them there. A <<<<<<< HEAD that reaches main is the classic sign of a conflict resolved in a hurry and, depending on the language, it either breaks the build immediately or, worse, it doesn't.

You can also control how much information is shown and how it is presented. For example: by default Git shows you the two candidates, whereas the zdiff3 style also shows the merge base, that is, what that region looked like before either branch touched it:

git config --global merge.conflictStyle zdiff3

Seeing what you need to resolve

The moment a merge stops, git status becomes your to-do list, and it is the command worth coming back to again and again. It names the files blocking the merge and even prints the next command you should run:

git status
1
2
3
4
5
6
7
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:   file.txt

The files under Unmerged paths are all the work there is. As you resolve each one and git add it, it leaves that list, so git status shrinks as you work and an empty list means you are done. If you only want the names, without the text around them:

git diff --name-only --diff-filter=U   # only the files still unresolved

The label in front of each file tells you what kind of conflict it is, and not all of them are "both edited the same lines":

  • both modified: both branches changed the same file. The usual case, and the one with <<<<<<< markers inside.
  • both added: both branches created a file at the same path with different content. It also has markers.
  • deleted by us / deleted by them: one side edited the file while the other deleted it. There are no markers here, so there is nothing to edit; you choose between keeping the file (git add) or accepting the deletion (git rm).

Tip

Resolve the files one at a time and git add each one immediately, instead of editing them all and staging them at the end. That way git status works as a to-do list that keeps shrinking, and you never wonder whether the file you fixed ten minutes ago was staged.

With a conflicting file open, git diff and its two variants save you from reading the raw markers to work out what changed:

1
2
3
git diff             # the conflicting regions, against both sides at once
git diff --ours      # your working file compared only with your side
git diff --theirs    # your working file compared only with the other side

Note

Aborting is still cheap while the conflict is unresolved: Git has quietly saved both original versions of every file, and your branch hasn't moved. Nothing you had committed is at risk while you are here, so there is never a reason to rush the resolution: git merge --abort always takes you back to the moment before you started.

Resolving a conflict by hand

The resolution cycle is always the same three steps, repeated once for each conflicting file:

  1. Open the file and decide what it should finally contain. You are not limited to picking one side, since the right answer is often a combination of both, or something neither branch wrote. The markers are just a way of presenting the options.
  2. Remove the markers. All of them, that is, <<<<<<<, =======, ||||||| and >>>>>>>, along with the candidate lines you have discarded. What remains must be the file exactly as you want it, as if the conflict had never existed.
  3. Stage it with git add. This is the step people forget, and it is the important one, since staging is how you tell Git this file is resolved. Until you do, the file stays under Unmerged paths and the merge refuses to finish.

Once all the files are staged, you close the merge:

git merge --continue     # finish the merge, opening the editor for the message
git commit               # equivalent, since Git knows a merge is in progress

Both write the same merge commit with its two parents, exactly as described in Chapter 4, and --continue is simply the explicit way of saying it.

Important

git add on a conflicting file means "I've resolved it", and Git believes you. It doesn't read the file, it doesn't look for leftover markers and it doesn't check that the result compiles. So the habit worth acquiring is running git diff --cached before finishing the merge, since it shows exactly what you are about to commit and a stray <<<<<<< can't slip past you there.

Tip

Resolve the files one at a time and stage them immediately, instead of editing them all and adding them at the end. That way git status works as a to-do list that keeps shrinking, and you always know how much you have left.

When one side simply wins

Sometimes there is nothing to combine, since one of the two versions is clearly the one you want. Typing it out by hand is unnecessary, because Git still has both versions in the index and can write either of them for you:

1
2
3
4
git checkout --ours <file>     # keep the version from the branch you are on
git checkout --theirs <file>   # keep the version from the branch you are merging in
git restore --ours <file>      # the modern way to write the same thing
git restore --theirs <file>    # ditto

These commands overwrite the file on disk with one whole side, markers included, so afterwards you still need to git add it to mark it as resolved.

Caution

--ours does not mean "mine" and --theirs does not mean "the other person's", since both are defined relative to HEAD. --ours is always the branch you are on and --theirs is always the branch named in the git merge command, whoever wrote it. This gets genuinely confusing with the rebase in Chapter 8, where the two swap meanings, so it is worth reading them as the receiving branch and the incoming branch rather than as possessives.

There is also a way to state that preference at the start of the merge, before any marker is written:

git merge -X ours <branch>     # in every conflicting region, keep your side
git merge -X theirs <branch>   # in every conflicting region, keep theirs

Read them as tie-breakers rather than filters. Everything that integrates cleanly is integrated normally, and the option only decides the regions that would otherwise have stopped the merge, so -X theirs still keeps all your work the other branch didn't touch.

Warning

Don't confuse -X ours with -s ours. The first is the tie-breaker we have just described, whereas the second is a strategy that completely discards the other branch's content and records a merge commit whose tree is identical to yours. -s ours has legitimate uses, e.g. marking a branch as merged without taking its code, but running it by accident silently throws away everything the other side did, and the history will claim it was merged.

Ways out and safety nets

A conflict never has to be resolved right now, and this is worth taking on board early, since it removes the pressure that makes people resolve badly:

git merge --abort      # cancel the merge and restore the state before running it
git reset --merge      # the same idea, also discarding the changes the merge introduced

--abort puts your branch, your index and your working directory back exactly where they were, and it works precisely because, as we saw earlier, HEAD never moved and both versions were already stored as objects. Nothing is lost by aborting, so a merge that looks worse than expected can always be postponed until you have talked to whoever wrote the other side.

Important

The only thing --abort can't rescue is uncommitted work you had in the working directory when the merge started, since that content was never recorded anywhere. It is the same rule from Chapter 1, that is, only what has been committed is safe, which is why Chapter 3 insisted on running git status before operations that rewrite your files.

Finally, there is a convenience worth knowing if you find yourself resolving the same conflict over and over, something that happens on long-lived branches that are merged repeatedly:

git config --global rerere.enabled true

rerere stands for reuse recorded resolution. With it enabled, Git remembers how you resolved a specific conflicting region and, the next time the same conflict appears, applies your previous answer on its own. It doesn't change anything in the mechanics described above, so it is best understood as Git taking notes while you work.

Tools that make it easier

Everything above can be done with any text editor, since the markers are plain text, and doing it that way at least once is the best way to understand what is going on. For day-to-day work, however, a three-pane view is more comfortable, since it puts your side, theirs and the result next to each other instead of stacked inside a single file:

git mergetool                    # open the conflicting files in the configured tool
git config --global merge.tool <name>   # choose it once, e.g. vimdiff, meld, vscode

VSCode ships with one, and it appears on its own when you open a conflicting file, offering Accept Current Change, Accept Incoming Change, Accept Both and a proper side-by-side merge editor. Those four buttons correspond exactly to what we have described, that is, --ours, --theirs, keeping both blocks and editing by hand. Chapter 10 covers the extensions that make this and the rest of the daily work more pleasant.

Note

Whatever tool you use, the contract with Git doesn't change. The tool writes the resolved file, and you still have to stage it and finish the merge, although most tools run the git add for you when you save.

Avoiding conflicts in the first place

Conflicts can't be eliminated, since two people working on the same code will end up touching the same lines, but how often they happen and how hard they are depends almost entirely on habits:

  • Keep branches short. A branch that lives two hours has drifted from main by two hours of changes, whereas a branch that lives two months has to reconcile everything that has happened in the meantime. Most painful conflicts are really the symptom of a branch left open too long.

  • Bring main into your branch regularly. Running git merge main while on your feature branch settles the disagreements in small doses, on your own branch, where a mistake costs nothing. It is the same operation from Chapter 4 in the opposite direction, and doing it often is what keeps the final merge boring.

  • Agree on formatting and let a tool enforce it. A reformatting pass rewrites every line of a file, so it conflicts with absolutely everything, and the conflict is pure noise since no logic has changed. Whenever you can, keep formatting commits separate from the ones that change behaviour.

  • Keep generated files out of the history. Builds, lock files rewritten on every install and editor settings generate conflicts constantly and are never worth resolving, which is exactly what the .gitignore from Chapter 2 is for.

  • Split the work by file when you can. Two people on the same function will have conflicts, whereas two people on different modules almost never will. This is coordination more than Git, but it is the habit with the biggest effect.

Tip

When a conflict is genuinely hard, that is, both sides rewrote the same logic in incompatible ways, stop and talk to whoever wrote the other side instead of choosing on your own. The markers show you two texts, whereas the real question is which of the two intentions the project wants, and Git doesn't have that information.

Summary

Command What it does
git status Lists the conflicting files under Unmerged paths
git diff --name-only --diff-filter=U Prints only the files still unresolved
git add <file> Marks a conflicting file as resolved
git rm <file> Resolves a modify/delete conflict by accepting the deletion
git merge --continue Finishes the merge once everything is resolved and staged
git merge --abort Cancels the merge and restores the previous state
git reset --merge Cancels the merge, also discarding the changes it introduced
git checkout --ours <file> / git restore --ours <file> Keeps the version from the branch you are on
git checkout --theirs <file> / git restore --theirs <file> Keeps the version from the branch you are merging in
git merge -X ours <branch> / -X theirs <branch> Resolves every conflicting region in favour of one side
git ls-files -u Shows the three versions the index keeps during a conflict
git mergetool Opens the conflicting files in a three-pane merge tool
git config --global merge.conflictStyle zdiff3 Adds the merge base to the markers
git config --global rerere.enabled true Remembers your resolutions and reapplies them

With this, the local part is complete. We can record work, read it and undo it, open parallel lines, integrate them and resolve the disagreements integration produces, all inside a single .git folder on a single machine. That last restriction is the one Chapter 6 removes by introducing remotes, that is, how a repository talks to a copy of itself that lives somewhere else, what clone, fetch, pull and push really move between them, and why origin/main is a branch you can look at but never be on.

See also