12

Is there any way to review list of conflicts (names of conflicting files and number of conflicts in it)?

The only thing I have discovered is to review pre-created .git/MERGE_MSG file... but this is not what I really searching for...

shytikov
  • 9,155
  • 8
  • 56
  • 103

4 Answers4

22

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
Community
  • 1
  • 1
me_and
  • 15,158
  • 7
  • 59
  • 96
  • Can you please also put a command to show just number of conflicted files . In addition to the number of conflicts in each file. ?. Like , git count-conflicted ---> says count of all files conflicted ( give some integer) – rinilnath Apr 11 '22 at 07:21
1

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.

kostix
  • 51,517
  • 14
  • 93
  • 176
  • Yes, but... is there any switch to show *only* conflicting files? – shytikov Jun 13 '12 at 13:08
  • @AlexeyShytikov, I don't know offhand, but from the manual, it looks like `git status --short` is able to code the status of each file listed using two characters -- look at the "Short Format" section. I think it's possible to wrap a call to `git status --short` in a shell script which would `sed` or `grep` only the relevant bits of output. – kostix Jun 13 '12 at 13:51
  • 1
    On the other hand, the answer @me_and gave requires scripting anyway so may be it's more logical to take his approach (and thus implement your own porcelain command). – kostix Jun 13 '12 at 13:52
  • Using `git status --porcelain` with a `sed` is the approach I used to take, but `git status` is a much slower operation than `git ls-files`, since the former needs to compare the working copy to the index. – me_and Jun 13 '12 at 16:56
1

TL;DR:

This command will give you a list of all files which had a conflict + the number of conflicts on each file:

git --no-pager diff --name-only --diff-filter=U |xargs grep -c '^=======$'



List all files affected by conflicts:

git --no-pager diff --name-only --diff-filter=U

This will give you a list with all unmerged files (source)


Get the number of files affected by conflicts:

git --no-pager diff --name-only --diff-filter=U | wc -l

List all files affected by conflicts + the number of conflicts in each file:

git --no-pager diff --name-only --diff-filter=U |xargs grep -c '^=======$'

This will give you an output similar to the following (where foo.txt has 2 conflicts and bar.txt has 5 conflicts):

path/to/foo.txt:2
path/to/bar.txt:5


Also, as @me_and suggests in his answer, it can also be helpful to create an alias.

Community
  • 1
  • 1
ludovico
  • 2,103
  • 1
  • 13
  • 32
0

Here is something useful that works on bash 4. You can add all sorts of single file stats to this, e.g. the number of lines of code that represent the number of conflicts.

#!/usr/bin/env bash

shopt -s globstar
for theFile in ./**/*; do
   if [ -f "$theFile" ]
     then
        conflicts=`grep -c '^=======$' "$theFile"`
        if [ "$conflicts" != "0" ]
          then
            echo "$theFile $conflicts"
        fi
   fi
done
Dion Truter
  • 7,592
  • 1
  • 19
  • 9