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...
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
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.
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 '^=======$'
git --no-pager diff --name-only --diff-filter=U
This will give you a list with all unmerged files (source)
git --no-pager diff --name-only --diff-filter=U | wc -l
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.
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