git found out number of conflicts / list of conflicts in working folder

git status shows files which are failed to be merged automatically and have conflicts, with the hint of how to record the resolved state of such files.


Edit: Of course, the easy, obvious and over-engineering free answer is git status, as kostix notes. The disadvantage of this is that git status checks the status of the index compared to the working copy, which is slow, whereas the below only checks the index, a much faster operation.

To get the names of the files that are conflicted, use git ls-files --unmerged.

$ git ls-files --unmerged
100755 f50c20668c7221fa6f8beea26b7a8eb9c0ae36e4 1       path/to/conflicted_file
100755 d0f6000e67d81ad1909500a4abca6138d18139fa 2       path/to/conflicted_file
100755 4cb5ada73fbe1c314f68c905a62180c8e93af3ba 3       path/to/conflicted_file

For ease, I have the following in my ~/.gitconfig file (I can't claim credit, but I can't remember the original source):

[alias]
    conflicts = !git ls-files --unmerged | cut -f2 | sort -u

This gives me:

$ git conflicts
path/to/conflicted_file

To work out the number of conflicts in a single file, I'd just use grep for the ======= part of the conflict marker:

$ grep -c '^=======$' path/to/conflicted_file
2

You could add the following to your ~/.gitconfig as well as the conflicts line above:

[alias]
    count-conflicts = !grep -c '^=======$'
    count-all-conflicts = !grep -c '^=======$' $(git conflicts)

This will give you:

$ git conflicts
path/to/a/conflicted_file
path/to/another/different_conflicted_file

$ git count-conflicts path/to/a/conflicted_file
2

$ git count-all-conflicts
5

Tags:

Git

Dvcs