Learn

Resolving a conflict

Git

When Git cannot decide, you have to. Step by step.

Prerequisites

When two branches modify the same lines in the same file, Git cannot merge automatically and asks you to decide. Conflict resolution is scary the first time, much less scary the second.

How a conflict happens

Imagine: on main, line 1 of README.md says "Hello". On your branch my-feature, you changed it to "Hi". Meanwhile, someone on main changed it to "Hey". Git does not know which one to keep.

When you run the merge:

shell
git switch main
git merge my-feature

Git stops with a message: CONFLICT (content): Merge conflict in README.md.

Reading the conflict markers

Open the conflicted file. You see something like this:

<<<<<<< HEAD
Hey
=======
Hi
>>>>>>> my-feature
  • <<<<<<< HEAD: start of the conflict block, version from the current branch (here main).
  • =======: separator between the two versions.
  • >>>>>>> my-feature: end of the block, version from the incoming branch.

Resolve the conflict

Edit the file manually to keep what you want. Delete all three markers and write the final version:

Hello and hey

Or keep one of the two versions, or write something completely different. You decide.

Finalize the resolution

Once all conflicted files are edited, stage them and commit:

shell
git add README.md
git commit -m "resolve conflict in README.md"

Git auto-generates a merge commit message, which you can accept or edit.

To see which files are still in conflict at any point:

shell
git status

Conflicted files appear under "both modified". As long as any remain, the merge is not complete.

Abort a merge in progress

If you realize you took the wrong approach and want to cancel everything:

shell
git merge --abort

Git restores the exact state from before the merge. Nothing is lost.

git-scm.com - git merge

Related

See also · ideEssential extensions

Check off steps to unlock what comes next

Back to course