What's the simplest way to list conflicted files in Git?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The simplest way to list conflicted files in Git is git diff --name-only --diff-filter=U. This outputs just the filenames of unmerged (conflicted) files, one per line, with no other noise. For a more detailed view that also shows staged and unstaged changes, git status works but requires you to scan the output for the "Unmerged paths" section. This guide covers both approaches plus scripting patterns, merge tool integration, and strategies for large-scale conflict resolution.
Quick Reference
Method 1: git diff with Unmerged Filter
This is the most scriptable and cleanest approach:
Output:
The --diff-filter=U flag tells Git to show only files with an "Unmerged" status. Combined with --name-only, you get a clean list with no extra information. This is ideal for scripting because each line is exactly one file path.
To also see the type of conflict (both modified, added by both, deleted by us, etc.):
Output:
Method 2: git status
git status is more verbose but gives you context about the entire working tree:
Output during a merge conflict:
The conflicted files appear under "Unmerged paths". The label tells you the conflict type: "both modified", "added by us", "added by them", "deleted by us", "deleted by them", etc.
For a short-format output that is easier to parse:
Output:
Files with UU, AA, DU, UD, or DD in the first two columns are conflicted.
Method 3: git ls-files for Low-Level Queries
For scripting or advanced workflows, git ls-files can list files with unmerged index entries:
Output:
The numbers 1, 2, and 3 represent the three stages of a merge conflict:
| Stage | Meaning |
| 1 | Common ancestor (base) |
| 2 | Current branch (ours) |
| 3 | Incoming branch (theirs) |
To get just unique filenames from this output:
Comparison of Methods
| Method | Output | Best For |
git diff --name-only --diff-filter=U | Clean filename list | Scripts, piping to other commands |
git status | Full working tree context | Interactive use, understanding overall state |
git status --short | Compact two-column format | Quick visual scan |
git ls-files --unmerged | Stage-level detail with object hashes | Advanced scripting, custom merge tools |
Scripting Patterns
Count Conflicted Files
Open All Conflicted Files in Your Editor
Resolve Conflicts by Accepting Theirs or Ours
When you know you want to accept all changes from one side:
CI Pipeline Conflict Check
In a CI script, you might want to fail the build if a merge produces conflicts:
Using git mergetool
After identifying conflicted files, git mergetool opens each one in a configured merge tool:
Configure your preferred tool in .gitconfig:
Popular merge tools include vimdiff, meld, kdiff3, opendiff (macOS), and VS Code's built-in merge editor.
Preventing Conflicts Proactively
While listing conflicts is essential for resolution, reducing their frequency is better:
- Rebase feature branches onto the target branch frequently to keep divergence small.
- Use
git fetchfollowed bygit mergeorgit rebaserather than letting branches diverge for weeks. - Break large changes into smaller, focused pull requests that are less likely to touch the same lines.
- Use
git merge-baseto understand the common ancestor and anticipate conflicts before merging.
Common Pitfalls
- Using
git diff --name-onlywithout--diff-filter=Uduring a merge. Without the filter, you get all changed files, not just the conflicted ones. - Confusing "both modified" with "both added". These are different conflict types and may require different resolution strategies.
git statustells you which type you are dealing with. - Forgetting to
git addafter resolving a conflict. Editing the file removes the conflict markers, but Git still considers it unmerged until you stage it withgit add. - Running
git commitbefore resolving all conflicts. Git will refuse to commit if unmerged files remain. Usegit diff --name-only --diff-filter=Uto check for stragglers. - Assuming
git statusoutput format is stable for parsing. The human-readable output can change between Git versions. For scripts, usegit status --porcelainorgit diff --name-only --diff-filter=U.
Summary
git diff --name-only --diff-filter=Uis the simplest and most scriptable way to list conflicted files.git statusprovides richer context, showing conflict types and the overall working tree state.git ls-files --unmergedgives low-level stage information for advanced tooling.- Pipe the filename list to editors,
git checkout --theirs/--ours, or merge tools for efficient bulk resolution. - Always
git addresolved files and verify no conflicts remain before committing.
Related reading
- What's the simplest way to list conflicted files in Git?
- What''s the strategy for handling CRLF carriage return, line feed with Git?
- When coding in TypeScript should the generated .js files be committed to git?
- When do you use Git rebase instead of Git merge?
- When does Java's Thread.sleep throw InterruptedException?
- When I catch an exception, how do I get the type, file, and line number?
- When does Git refresh the list of remote branches?
- When does Git refresh the list of remote branches?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.