10

有没有办法查看冲突列表(冲突文件的名称和其中的冲突数量)?

我发现的唯一一件事就是查看预先创建的.git/MERGE_MSG文件……但这不是我真正要寻找的……

4

4 回答 4

20

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
于 2012-06-13T12:08:32.300 回答
1

git status显示自动合并失败且有冲突的文件,并提示如何记录此类文件的已解决状态。

于 2012-06-13T12:41:06.153 回答
1

TL;博士:

此命令将为您提供所有有冲突的文件的列表 + 每个文件的冲突数:

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



列出所有受冲突影响的文件:

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

这将为您提供一个包含所有未合并文件的列表(


获取受冲突影响的文件数:

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

列出所有受冲突影响的文件 + 每个文件中的冲突数:

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

这将为您提供类似于以下的输出(其中 foo.txt 有 2 个冲突,而 bar.txt 有 5 个冲突):

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


此外,正如@me_and 在他的回答中所建议的那样,创建别名也很有帮助。

于 2019-08-16T17:18:24.910 回答
0

这是适用于 bash 4 的一些有用的东西。您可以在其中添加各种单个文件统计信息,例如代表冲突数量的代码行数。

#!/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
于 2014-03-27T16:16:15.923 回答