Git
Conflict Resolution
File Management
Programming
Troubleshooting

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.

Browse interview questions

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

bash
1# Just the conflicted filenames (cleanest output)
2git diff --name-only --diff-filter=U
3
4# Detailed working tree status including conflicts
5git status
6
7# Conflicted files with their conflict type
8git diff --name-status --diff-filter=U

Method 1: git diff with Unmerged Filter

This is the most scriptable and cleanest approach:

bash
git diff --name-only --diff-filter=U

Output:

plaintext
src/config.yaml
src/models/user.py
tests/test_auth.py

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.):

bash
git diff --name-status --diff-filter=U

Output:

plaintext
U	src/config.yaml
U	src/models/user.py
U	tests/test_auth.py

Method 2: git status

git status is more verbose but gives you context about the entire working tree:

bash
git status

Output during a merge conflict:

plaintext
1On branch feature/auth
2You have unmerged paths.
3  (fix conflicts and run "git commit")
4  (use "git merge --abort" to abort the merge)
5
6Changes to be committed:
7    modified:   src/app.py
8
9Unmerged paths:
10  (use "git add <file>..." to mark resolution)
11    both modified:   src/config.yaml
12    both modified:   src/models/user.py
13    both modified:   tests/test_auth.py

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:

bash
git status --short

Output:

plaintext
1M  src/app.py
2UU src/config.yaml
3UU src/models/user.py
4UU tests/test_auth.py

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:

bash
git ls-files --unmerged

Output:

plaintext
100644 abc1234 1	src/config.yaml
100644 def5678 2	src/config.yaml
100644 ghi9012 3	src/config.yaml

The numbers 1, 2, and 3 represent the three stages of a merge conflict:

StageMeaning
1Common ancestor (base)
2Current branch (ours)
3Incoming branch (theirs)

To get just unique filenames from this output:

bash
git ls-files --unmerged | cut -f2 | sort -u

Comparison of Methods

MethodOutputBest For
git diff --name-only --diff-filter=UClean filename listScripts, piping to other commands
git statusFull working tree contextInteractive use, understanding overall state
git status --shortCompact two-column formatQuick visual scan
git ls-files --unmergedStage-level detail with object hashesAdvanced scripting, custom merge tools

Scripting Patterns

Count Conflicted Files

bash
git diff --name-only --diff-filter=U | wc -l

Open All Conflicted Files in Your Editor

bash
1# VS Code
2git diff --name-only --diff-filter=U | xargs code
3
4# Vim
5git diff --name-only --diff-filter=U | xargs vim

Resolve Conflicts by Accepting Theirs or Ours

When you know you want to accept all changes from one side:

bash
1# Accept all incoming changes (theirs)
2git diff --name-only --diff-filter=U | xargs git checkout --theirs
3git diff --name-only --diff-filter=U | xargs git add
4
5# Accept all current changes (ours)
6git diff --name-only --diff-filter=U | xargs git checkout --ours
7git diff --name-only --diff-filter=U | xargs git add

CI Pipeline Conflict Check

In a CI script, you might want to fail the build if a merge produces conflicts:

bash
1git merge origin/main --no-commit --no-ff 2>/dev/null
2CONFLICTS=$(git diff --name-only --diff-filter=U | wc -l)
3if [ "$CONFLICTS" -gt 0 ]; then
4    echo "Merge would produce $CONFLICTS conflicts:"
5    git diff --name-only --diff-filter=U
6    git merge --abort
7    exit 1
8fi
9git merge --abort

Using git mergetool

After identifying conflicted files, git mergetool opens each one in a configured merge tool:

bash
1# Open all conflicts in your configured merge tool
2git mergetool
3
4# Open a specific conflicted file
5git mergetool src/config.yaml

Configure your preferred tool in .gitconfig:

ini
1[merge]
2    tool = vimdiff
3[mergetool]
4    keepBackup = false

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 fetch followed by git merge or git rebase rather 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-base to understand the common ancestor and anticipate conflicts before merging.
bash
1# Find common ancestor between two branches
2git merge-base feature/auth main
3
4# Preview what a merge would look like without committing
5git merge --no-commit --no-ff origin/main
6git diff --name-only --diff-filter=U
7git merge --abort

Common Pitfalls

  • Using git diff --name-only without --diff-filter=U during 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 status tells you which type you are dealing with.
  • Forgetting to git add after resolving a conflict. Editing the file removes the conflict markers, but Git still considers it unmerged until you stage it with git add.
  • Running git commit before resolving all conflicts. Git will refuse to commit if unmerged files remain. Use git diff --name-only --diff-filter=U to check for stragglers.
  • Assuming git status output format is stable for parsing. The human-readable output can change between Git versions. For scripts, use git status --porcelain or git diff --name-only --diff-filter=U.

Summary

  • git diff --name-only --diff-filter=U is the simplest and most scriptable way to list conflicted files.
  • git status provides richer context, showing conflict types and the overall working tree state.
  • git ls-files --unmerged gives 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 add resolved files and verify no conflicts remain before committing.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.