Chapter 6 · Lesson 6 of 10
Git for deployers

Resolve Git merge conflicts step by step

A merge conflict happens when two branches changed the same lines, so you edit the marked file to keep the right text, run git add, and finish with git commit.

By the end you will

Read conflict markers, resolve a conflict by hand, finish or abort the merge, and handle a conflict during a rebase.

Time
15 min
Level
Intermediate
Check
4 questions
You need
  • A repository where you can create two branches

A merge conflict means Git could not combine two changes on its own, because both branches edited the same lines. Git stops, writes markers into the file, and waits. You edit the file to the version you want, remove the markers, run git add on it, and finish with git commit. If you would rather not deal with it now, git merge --abort puts everything back.

How it works#

Git merges by comparing three versions of each file: the common ancestor, your branch and the incoming branch. If only one side changed a region, Git takes that change. If both changed the same region differently, Git cannot know which is right, so it reports CONFLICT (content): Merge conflict in <file>, pauses the merge and leaves the file with both versions inside.

Conflicts are normal. They do not mean something is broken or that you did something wrong. They show up more when branches live a long time, or when two people edit the same file, such as a config or a lock file. The branching steps are in Git branches and merging.

Step 1: Start a merge that conflicts#

bash
git switch main
git merge feature-colors

Git prints something like:

output
Auto-merging app.conf
CONFLICT (content): Merge conflict in app.conf
Automatic merge failed; fix conflicts and then commit the result.

Your repository is now in the middle of a merge. Nothing is lost.

Step 2: See what is conflicted#

bash
git status

Look for You have unmerged paths. and the section Unmerged paths: listing files as both modified: app.conf. Files without conflicts that merged cleanly are already staged. git diff shows the conflicting regions, and git diff --name-only --diff-filter=U lists only the unmerged files.

Step 3: Read the conflict markers#

Open the file. A conflicted region looks like this:

output
<<<<<<< HEAD
color = blue
=======
color = green
>>>>>>> feature-colors
  • Between <<<<<<< HEAD and ======= is your current branch’s version (the branch you merged into).
  • Between ======= and >>>>>>> feature-colors is the incoming branch’s version.

Step 4: Edit the file#

Decide what the final text should be. You can keep one side, keep the other, or combine both. Then delete all three marker lines. For example:

output
color = green

Edit the whole file, since there can be several conflicted regions. Search for <<<<<<< to find each. Then build or run the tests, because a text merge can compile and still behave wrongly.

Step 5: Stage and commit#

bash
git add app.conf
git commit

git add tells Git the file is resolved. When no unmerged paths remain, git commit finishes the merge. Git opens an editor with a prepared message such as Merge branch 'feature-colors', and you can accept it as is.

Backing out with merge —abort#

If the conflict is bigger than you expected, or you merged the wrong branch:

bash
git merge --abort

This restores the state before the merge started. It only works while the merge is unfinished. After the commit, use revert or reset instead.

Conflicts during a rebase#

git rebase main replays your commits one by one on top of main. A conflict can stop it in the middle:

bash
# fix the file, then:
git add app.conf
git rebase --continue

The commands differ from a merge: you do not run git commit, and the markers use the reversed meaning. During a rebase, the “HEAD” side is the branch you are rebasing onto and the incoming side is your own commit being replayed. To skip a commit, use git rebase --skip, and to cancel the whole thing, git rebase --abort. Do not rebase branches that others have already pulled.

Using a merge tool#

For large conflicts a three-way visual tool helps:

bash
git mergetool

Git launches a configured tool such as vimdiff, meld or a graphical editor, and it leaves .orig backup files you should delete after. Set the tool with git config --global merge.tool vimdiff. On a headless server, plain editing of the markers is usually easier.

Conflicts are most common when a pull request goes stale, as described in the deploy workflow lesson, and you can start from your first repository if the basics are unfamiliar. Pulling from a remote can also trigger them, see push, pull and fetch. A rollback is simpler on a clean history. Conflicts also appear in a CI/CD flow when a pull request goes stale, so rebase or merge main into it early. The blog post Deploy an Express app with Docker on a VPS shows a full path from commit to server, and undo mistakes in Git covers backing out after the merge is committed.

Check it worked#

After the commit, git status should say nothing to commit, working tree clean, with no Unmerged paths. Search the repository for leftover markers:

bash
git grep -n -E '^(<<<<<<<|=======|>>>>>>>)'

No output means none remain. Also run your tests or start the app before pushing.

Edge cases and gotchas#

  • Markers in committed code. The most common mistake is committing with <<<<<<< still in the file. Run the git grep check above.
  • Binary files. Git cannot merge images or archives. Choose a side with git checkout --ours file or git checkout --theirs file, then git add.
  • Lock files conflict often. For package-lock.json or similar, accept one side and regenerate the file with your package manager.
  • Whitespace conflicts. Different line endings or indentation can conflict. git merge -X ignore-space-change can help, but review the result.
  • rerere. git config --global rerere.enabled true makes Git remember how you resolved a conflict and reapply it, which helps with repeated rebases.
  • Resolve on a branch, not on a server. Do not resolve conflicts on a production machine. Do it locally or in review, then deploy the result.

Troubleshooting#

SymptomLikely causeFix
CONFLICT (content): Merge conflict in app.confBoth branches edited the same linesEdit the file, git add, git commit
error: you need to resolve your current index firstUnmerged files still existResolve and stage them, or git merge --abort
fatal: You have not concluded your merge (MERGE_HEAD exists).A merge is waiting for youFinish it with git commit, or abort
error: Committing is not possible because you have unmerged files.Some conflicted file was not stagedRun git status, fix and git add each one
CONFLICT (modify/delete): app.conf deleted in HEAD and modified in featureOne side deleted, the other editedChoose with git rm app.conf or git add app.conf
fatal: There is no merge to abort (MERGE_HEAD missing).No merge in progressNothing to abort, check git status
error: could not apply 3f2a1c9... add colorsConflict during rebaseFix the file, git add, git rebase --continue
No files need merging from git mergetoolEverything is already resolvedCommit or continue the rebase

Quick reference#

bash
git merge feature            # may stop with conflicts
git status                   # list unmerged paths
# edit the file, remove <<<<<<<, =======, >>>>>>>
git add file
git commit                   # finish the merge
git merge --abort            # give up and go back
git rebase --continue        # during a rebase
git rebase --abort           # cancel a rebase
git mergetool                # open a merge tool

Next#

Share your work next with Git remotes, push, pull and fetch.

Check what you learned4 questions
1Which lines must you remove from a conflicted file before committing?
2You started a merge and want to go back to how things were. Which command?
3How do you tell Git a conflicted file is resolved?
4During a rebase conflict, how do you continue after fixing and staging the file?

Frequently asked questions

What does CONFLICT (content): Merge conflict in file mean?

Git could not combine two changes automatically because both branches edited the same part of the file. It stops and marks the file so you can choose what the result should be.

How do I abort a merge conflict in Git?

Run git merge --abort. It returns your branch and working tree to the state before you started the merge.

What do the <<<<<<<, ======= and >>>>>>> lines mean?

They are conflict markers. The part between <<<<<<< and ======= is your current branch's version, and the part between ======= and >>>>>>> is the incoming branch's version.

Do I commit after resolving a conflict?

Yes, for a merge. After you edit the file and run git add on it, git commit finishes the merge. During a rebase you run git rebase --continue instead.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios