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.
- Updated Nov 27, 2026
- Markdown
Read conflict markers, resolve a conflict by hand, finish or abort the merge, and handle a conflict during a rebase.
- 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#
git switch main
git merge feature-colorsGit prints something like:
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#
git statusLook 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:
<<<<<<< HEAD
color = blue
=======
color = green
>>>>>>> feature-colors- Between
<<<<<<< HEADand=======is your current branch’s version (the branch you merged into). - Between
=======and>>>>>>> feature-colorsis 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:
color = greenEdit 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#
git add app.conf
git commitgit 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:
git merge --abortThis 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:
# fix the file, then:
git add app.conf
git rebase --continueThe 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:
git mergetoolGit 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:
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 thegit grepcheck above. - Binary files. Git cannot merge images or archives. Choose a side with
git checkout --ours fileorgit checkout --theirs file, thengit add. - Lock files conflict often. For
package-lock.jsonor 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-changecan help, but review the result. - rerere.
git config --global rerere.enabled truemakes 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#
| Symptom | Likely cause | Fix |
|---|---|---|
CONFLICT (content): Merge conflict in app.conf | Both branches edited the same lines | Edit the file, git add, git commit |
error: you need to resolve your current index first | Unmerged files still exist | Resolve and stage them, or git merge --abort |
fatal: You have not concluded your merge (MERGE_HEAD exists). | A merge is waiting for you | Finish it with git commit, or abort |
error: Committing is not possible because you have unmerged files. | Some conflicted file was not staged | Run git status, fix and git add each one |
CONFLICT (modify/delete): app.conf deleted in HEAD and modified in feature | One side deleted, the other edited | Choose with git rm app.conf or git add app.conf |
fatal: There is no merge to abort (MERGE_HEAD missing). | No merge in progress | Nothing to abort, check git status |
error: could not apply 3f2a1c9... add colors | Conflict during rebase | Fix the file, git add, git rebase --continue |
No files need merging from git mergetool | Everything is already resolved | Commit or continue the rebase |
Quick reference#
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 toolRelated lessons#
- Git branches and merging: how merges produce conflicts.
- Undo mistakes in Git: backing out after the merge is committed.
- Git remotes, push, pull and fetch: conflicts that appear when you pull.
- Read and edit files on Linux: editing the conflicted file with nano or vim.
Next#
Share your work next with Git remotes, push, pull and fetch.
Check what you learned
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
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.