How to Resolve Git Merge Conflicts
When git merge of another branch stops with CONFLICT (content): Merge conflict in ..., rewrite the conflicting file by hand, then run git add and git commit to finish the merge. If you want to cancel the merge itself, git merge --abort returns you to the state before the merge. Below, I show how to read a conflicted file and the output before and after each step I ran in my test environment.
The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>) and fixed the commit dates with environment variables so the output would be the same each time. I only used repositories I created locally. I did not test conflicts during git pull with a remote, git mergetool or the conflict-resolution features of editors, or Windows and macOS.
- 1. Two branches that conflict
- 2. What to do when git merge shows CONFLICT
- 3. How to read a conflicted file
- 4. You cannot commit while the conflict is still there
- 5. Rewrite the file, then finish with add and commit
- 6. Cancel the merge and return to the original state
- 7. Use one branch's whole version of a file
- 8. Show the content from before the branches diverged
Two branches that conflict
I started with a repository containing index.html, where the main and schedule branches each have their own commits.
$ git log --graph --oneline --all
* 0f9bc80 土曜も更新する
| * e851e49 フッターの年を直す
| * f5bc99d 更新日を月曜にする
|/
* 16b5a97 最初のコミット
Line 2 of the original index.html said "I update every Friday." On main, I changed this line to "I update every Friday and Saturday." On schedule, I changed the same line to "I update every Monday." Separately, on schedule, I also changed the year in the footer on the last line from 2025 to 2026. The index.html on main before the merge is shown below.
$ cat index.html
<h1>ねこノート</h1>
<p>毎週金曜と土曜に更新します</p>
<ul>
<li>記事の一覧</li>
</ul>
<footer>2025 ねこノート</footer>
What to do when git merge shows CONFLICT
When I merged schedule while on main, Git gave up on the automatic merge and stopped.
$ git merge schedule
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
$ git status
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: index.html
no changes added to commit (use "git add" and/or "git commit -a")
The files listed under Unmerged paths in git status are the conflicted files. both modified means both branches changed the same file and the changes have not been merged yet. When I caused the same conflict another time and checked it with the short form git status --short, it showed the following.
$ git status --short
UU index.html
The official git status reference describes UU as "unmerged, both modified". As the output shows, you have two options: fix the conflict and run git commit, or cancel the merge with git merge --abort.
How to read a conflicted file
When I opened the conflicted file, the conflicting part was separated by three kinds of marker lines: <<<<<<<, =======, and >>>>>>>.
$ cat index.html
<h1>ねこノート</h1>
<<<<<<< HEAD
<p>毎週金曜と土曜に更新します</p>
=======
<p>毎週月曜に更新します</p>
>>>>>>> schedule
<ul>
<li>記事の一覧</li>
</ul>
<footer>2026 ねこノート</footer>
The part from <<<<<<< HEAD to ======= is the content of the branch you are on (main). The part from ======= to >>>>>>> schedule is the content of schedule, the branch you are merging in. The footer on the last line was changed only on the schedule side, so it did not conflict and was already updated to 2026. The official git merge reference on how conflicts are presented says that parts changed on only one side are taken over as they are, and only when both sides changed the same part does Git keep both versions and ask you to decide.

While a conflict is unresolved, the original version from each side stays in the index (stage). You can display each version with git show by adding :1: (the common ancestor, before the two branches diverged), :2: (the current branch), or :3: (the branch being merged in).
$ git show :1:index.html | sed -n 2p
<p>毎週金曜に更新します</p>
$ git show :2:index.html | sed -n 2p
<p>毎週金曜と土曜に更新します</p>
$ git show :3:index.html | sed -n 2p
<p>毎週月曜に更新します</p>
You cannot commit while the conflict is still there
If you run git commit without git add on the conflicted file, it stops with an error.
$ git commit --no-edit
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
U index.html
To check whether you missed deleting a marker line, run git diff --check. If a marker line remains, its line number is shown.
$ git diff --check; echo "exit=$?"
index.html:2: leftover conflict marker
index.html:4: leftover conflict marker
index.html:6: leftover conflict marker
exit=2
Rewrite the file, then finish with add and commit
You do not have to pick one side. Here I kept both changes and made the line "I update on Monday and Saturday." The index.html after I deleted the three marker lines and merged the two lines into one is shown below. In the test, I rewrote the file with printf.
$ cat index.html
<h1>ねこノート</h1>
<p>毎週月曜と土曜に更新します</p>
<ul>
<li>記事の一覧</li>
</ul>
<footer>2026 ねこノート</footer>
$ git diff --check; echo "exit=$?"
exit=0
If no marker lines remain, git diff --check prints nothing. After I ran git add on the rewritten file, the output of git status changed.
$ git add index.html
$ git status
On branch main
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: index.html
$ git commit --no-edit
[main f267e44] Merge branch 'schedule'
$ git log --graph --oneline
* f267e44 Merge branch 'schedule'
|\
| * e851e49 フッターの年を直す
| * f5bc99d 更新日を月曜にする
* | 0f9bc80 土曜も更新する
|/
* 16b5a97 最初のコミット
--no-edit makes the commit without opening an editor, using the message that Git prepared (Merge branch 'schedule'). This created a merge commit joining the two branches, and the conflict was resolved. According to the official git merge reference on how to resolve conflicts, you can use git merge --continue instead of the final git commit. It checks that a merge is in progress before it runs git commit.
Cancel the merge and return to the original state
If you look at the conflict and decide not to merge now, run git merge --abort. For the test, I undid the merge commit above with git reset --hard HEAD~1 to return to the state before the merge, then ran git merge schedule again to cause the conflict and tried it.
$ git merge schedule
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
$ git merge --abort
$ git status
On branch main
nothing to commit, working tree clean
$ cat index.html
<h1>ねこノート</h1>
<p>毎週金曜と土曜に更新します</p>
<ul>
<li>記事の一覧</li>
</ul>
<footer>2025 ねこノート</footer>
$ git log --oneline -1
0f9bc80 土曜も更新する
The marker lines are gone, and so is the footer's 2026 that had been merged automatically. index.html returned to the main content from before the merge.
According to the official git merge reference on –abort, if you start a merge with uncommitted changes, git merge --abort may not be able to restore those changes. The reference recommends committing your changes, or setting them aside with git stash, before you merge.
Use one branch's whole version of a file
If you only need to keep one side's version, you can select the whole file with git checkout --theirs (the branch being merged in) or --ours (the current branch), instead of editing the file by hand. I caused the conflict again and used --theirs.
$ git checkout --theirs index.html
Updated 1 path from the index
$ cat index.html
<h1>ねこノート</h1>
<p>毎週月曜に更新します</p>
<ul>
<li>記事の一覧</li>
</ul>
<footer>2026 ねこノート</footer>
$ git add index.html
$ git commit --no-edit
[main dab42ef] Merge branch 'schedule'
The choice applies to the whole file, not only to the conflicting part. I caused the conflict again and chose --ours. Line 2 became the main content, but the footer, which had not conflicted, also went back to 2025 from main. The footer year that I changed on schedule disappeared from this file.
$ git checkout --ours index.html
Updated 1 path from the index
$ cat index.html
<h1>ねこノート</h1>
<p>毎週金曜と土曜に更新します</p>
<ul>
<li>記事の一覧</li>
</ul>
<footer>2025 ねこノート</footer>
In this case, I cancelled the merge with git merge --abort. Also, the official git checkout reference says that during git rebase or git pull --rebase, the sides that ours and theirs refer to can appear swapped.
Show the content from before the branches diverged
In the default display, you cannot see what a part looked like before the two branches diverged. If you set merge.conflictStyle to diff3 and then merge, the content from before the divergence is shown after a ||||||| line.
$ git config merge.conflictStyle diff3
$ git config --show-origin merge.conflictStyle
file:.git/config diff3
$ git merge schedule
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
$ cat index.html
<h1>ねこノート</h1>
<<<<<<< HEAD
<p>毎週金曜と土曜に更新します</p>
||||||| 16b5a97
<p>毎週金曜に更新します</p>
=======
<p>毎週月曜に更新します</p>
>>>>>>> schedule
<ul>
<li>記事の一覧</li>
</ul>
<footer>2026 ねこノート</footer>
Since the original text said "every Friday," you can tell that main added Saturday and schedule changed Friday to Monday. I set the configuration without --global, so git config --show-origin shows that the setting is written in this repository's .git/config. According to the merge.conflictStyle reference, the other value, zdiff3, is similar to diff3, but it moves lines that are the same on both sides at the start and end of the conflicting part outside the markers.



