What's the simplest way to list conflicted files in Git?

476,124

Solution 1

Use git diff, with name-only to show only the names, and diff-filter=U to only include 'Unmerged' files (optionally, relative to show paths relative to current working directory) .

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

Solution 2

git diff --check

will show the list of files containing conflict markers including line numbers.

For example:

> git diff --check
index-localhost.html:85: leftover conflict marker
index-localhost.html:87: leftover conflict marker
index-localhost.html:89: leftover conflict marker
index.html:85: leftover conflict marker
index.html:87: leftover conflict marker
index.html:89: leftover conflict marker

source : https://ardalis.com/detect-git-conflict-markers

Solution 3

Trying to answer my question:

No, there doesn't seem to be any simpler way than the one in the question, out of box.

After typing that in too many times, just pasted the shorter one into an executable file named 'git-conflicts', made accessible to git, now I can just: git conflicts to get the list I wanted.

Update: as Richard suggests, you can set up an git alias, as alternative to the executable

git config --global alias.conflicts '!git ls-files -u | cut -f 2 | sort -u'

An advantage of using the executable over the alias is that you can share that script with team members (in a bin dir part of the repo).

Solution 4

Here is a fool-proof way:

grep -H -r "<<<<<<< HEAD" /path/to/project/dir

Solution 5

git status displays "both modified" next to files that have conflicts instead of "modified" or "new file", etc

Share:
476,124
inger
Author by

inger

Updated on July 08, 2022

Comments

  • inger
    inger almost 2 years

    I just need a plain list of conflicted files.

    Is there anything simpler than:

    git ls-files -u  | cut -f 2 | sort -u
    

    or:

    git ls-files -u  | awk '{print $4}' | sort | uniq
    

    I guess I could set up a handy alias for that, however was wondering how pros do it. I'd use it to write shell loops e.g. to auto-resolve conflict, etc. Maybe replace that loop by plugging into mergetool.cmd?

  • inger
    inger over 12 years
    I felt the same at that point - thinking how the hell people don't need this, and seeing how trivial it was to workaround. However, I've been using git for 2 years now and honestly haven't run into that "limitation" once more. So maybe that's not that much of common usecase after all?
  • inger
    inger over 12 years
    That's true. However this particular question was about a plain list of conflicted files.. this might be an XY problem (I can't remember why I actually needed that conflict list, but the fact that I haven't needed it since might suggest that I should have followed a different approach back then. Not sure now.. I also was writing scripts for autoresolving java-import conflicts which needed this list, ie. non-interactive use)..
  • inger
    inger over 12 years
    "interactively because the list gets shorter as you fix the conflicts." Interesting. I've always used mergetool for that purpose.
  • Rafa
    Rafa over 12 years
    Oh, I hadn't understood that. I thought you wanted a "normal" list for "normal" use. Which is why I freaked out with your own code and your self-answer... then I realized the "both modified" thingy worked for me (and I assumed you just wanted the same as me, why shouldn't you? ;-P ) Thanks for the upvote though :)
  • mda
    mda over 12 years
    Note: You may need to search for ^UA and ^UD also, so the following pattern is more complete: "^U[UAD] "
  • Richard
    Richard about 12 years
    This is simple enough that you could set up an alias for it git config --global alias.conflicts "!git ls-files -u | cut -f 2 | sort -u" (the ! means run this shell command, rather than just a git command).
  • umop
    umop about 12 years
    Worth mentioning that you actually want 'single-quotes' instead of "double-quotes." Otherwise, the ! will be interpreted by your shell: git config --global alias.conflicts '!git ls-files -u | cut -f 2 | sort -u'
  • Database
    Database over 11 years
    I created an alias for this: git config --global alias.conflicts "diff --name-only --diff-filter=U"
  • Ascherer
    Ascherer over 10 years
    or just ^U to get everything starting with U
  • anthony sottile
    anthony sottile about 10 years
    This isn't sufficient. Conflicting files can have the following combinations: DD, AU, UD, UA, DU, AA, UU
  • Alexander Bird
    Alexander Bird about 10 years
    No. Git's index will still internally mark certain files as being in conflict even after the textual markers in the files are removed.
  • mda
    mda about 10 years
    @AnthonySottile: Can you explain the scenarios? I posted what worked for my case.
  • anthony sottile
    anthony sottile about 10 years
    @mda One example: Conflict where upstream modified, I deleted will have status DU
  • WoodenKitty
    WoodenKitty over 9 years
    Alongside Alexander's comment, it's still useful to see this as an option :) Please don't delete.
  • Pacerier
    Pacerier over 8 years
    @AnthonySottile, Am I missing something? Why even bother with grep in the first place?
  • Pacerier
    Pacerier over 8 years
    @CharlesBailey, Am I missing something? What's wrong with git status?
  • anthony sottile
    anthony sottile over 8 years
    My usecase was supporting older versions of git (I believe <1.5) which do not have the --diff-filter option.
  • xster
    xster over 8 years
    @Pacerier, it's just messier. If you had a million unconflicting merges and one conflicting merge, you'd want something succinct for output.
  • David Douglas
    David Douglas over 8 years
    Or to run within current working dir use a dot for path - grep -H -r "<<<<<<< HEAD" .
  • ShellFish
    ShellFish about 8 years
    Hehe, this is my way of doing it too. Adding a c nicely results the count of conflicts too! One note is that I'd use flags -Hrn this will also supply line number information.
  • sAguinaga
    sAguinaga almost 8 years
    @Jimothy how do you use that alias?
  • Database
    Database almost 8 years
    @sAguinaga: Simply run git conflicts
  • celticminstrel
    celticminstrel almost 8 years
    If you're using regex, I'd suggest [<=>]{7} instead of this. (Might need -E flag for that to work in grep.) Or, <{7} if you're not worried about dangling merge markers or want to count the conflicts. (You can also use git grep - then you don't need the -r flag.)
  • Timo
    Timo almost 7 years
    The H Flag means that the file name will be shown. r means recursive, If you do git grep, you put --files-with-matches instead of the H flag.
  • Database
    Database over 6 years
    @QZSupport You must first set up the alias, as indicated in my earlier comment: git config --global alias.conflicts "diff --name-only --diff-filter=U"
  • Koenigsberg
    Koenigsberg about 5 years
    Conflicts can include modified vs. deleted files which this solution will not cover.
  • Michael
    Michael about 5 years
    @AnthonySottile, are all those combinations guaranteed to be conflicted, or are some of them just "possibly" conflicted? Edit: The chart at Mateusz's link seems to indicate that they are guaranteed to be conflicts.
  • Michael
    Michael about 5 years
    I don't know if it's the shortest regex, but this gets all the conflicted combinations and none of the unconflicted combinations: grep -E "^([AUD][AU]|[UD]D) ". See regex101.com/r/Wh1fUi/1
  • Charles Duporge
    Charles Duporge about 5 years
    @ChristianMatthew diff-filter=U asks to show only files that are unmerged, the option can use other options like A for added files, M for modified files and so on. See doc for more info: git-scm.com/docs/git-diff#Documentation/…
  • aksanoble
    aksanoble almost 5 years
    This continues to show the file even after resolving the conflict. git diff --check works better.
  • Michael
    Michael almost 5 years
    @self Also ^(.U|U.|AA|DD).
  • CCJ
    CCJ over 4 years
    I've found git diff --check telling me about other (less serious) problems too, like trailing whitespace, so a git diff --check | grep -i conflict might be in order for OP's case
  • Amory
    Amory over 4 years
    It's worth noting that this only works when items haven't been staged (or unstaged) after arriving at the conflict. @cnlevy's answer (git diff --check) will find conflict markers for unstaged files.
  • wyattis
    wyattis over 4 years
    It's much faster to use git's grep command for large codebases because it will follow the rules configured in the .gitignore file. git grep -H -r "<<<<<< HEAD"
  • zefciu
    zefciu about 4 years
    @Pacerier nothing is wrong really. But there are situations, where you might want only the names of conflicting files. E.g. the command vim `git diff --name-only --diff-filter=U` will open all conflicting files in vim, so you can fix the conflicts one by one.
  • Josh Cooley
    Josh Cooley almost 4 years
    git diff --check uses the whitespace rules from core.whitespace. You can disable all of the whitespace checks during git invocation to just get conflict markers: git -c core.whitespace=-trailing-space,-space-before-tab,-indent-wi‌​th-non-tab,-tab-in-i‌​ndent,-cr-at-eol diff --check
  • Reed
    Reed almost 4 years
    For git stash apply, << HEAD will not be there. Instead `<< Updated Upstream
  • Reed
    Reed almost 4 years
    If you're IN your git dir, use grep -Hrn "<<<<<<< " "$(git rev-parse --show-toplevel)", and it will search from the root of your project
  • Kalinda Pride
    Kalinda Pride over 3 years
    There can also be merge conflicts where one branch deleted a file and the other modified it. These won't show up with git status | grep "both modified".
  • CervEd
    CervEd over 3 years
    nice! it also shows stuff like trailing ws <3
  • alper
    alper almost 3 years
    git diff --check returns empty even git diff --name-only --diff-filter=U returns files is it normal?
  • Connor Clark
    Connor Clark almost 3 years
    +1 for using an alias, but this removes the file paths (keeping only the basename) which makes piping to other programs (like git conflicts | xargs code impossible. should just drop the grep, like: stackoverflow.com/a/21541490/2788187
  • Dogunbound hounds
    Dogunbound hounds over 2 years
    This can be an ok solution, but really slows down with bigger projects.
  • Saurabh Bajaj
    Saurabh Bajaj over 2 years
    Good idea to create an alias. Just an info for others, to run alias you need to prefix git like this git <alias>. So in this case it would be git conflicts
  • TamusJRoyce
    TamusJRoyce over 2 years
    this doesn't work for committed code. not useful to pipelines where code is checked in
  • Markus
    Markus almost 2 years
    To disable pager in the git alias: git config --global alias.conflicts "!git --no-pager diff --name-only --diff-filter=U"