【问题标题】:git found out number of conflicts / list of conflicts in working foldergit发现工作文件夹中的冲突数量/冲突列表
【发布时间】:2012-06-16 08:38:09
【问题描述】:

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

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

【问题讨论】:

    标签: git dvcs


    【解决方案1】:

    编辑:当然,简单、明显和过度设计的免费答案是git status,如kostix notes。这样做的缺点是git status检查索引的状态与工作副本相比,速度很慢,而下面只检查索引,操作速度更快。

    要获取冲突文件的名称,请使用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
    

    为方便起见,我的~/.gitconfig 文件中有以下内容(我无法声明功劳,但我不记得原始出处):

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

    这给了我:

    $ git conflicts
    path/to/conflicted_file
    

    要计算单个文件中的冲突数量,我只需将grep 用作冲突标记的======= 部分:

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

    您可以将以下内容添加到您的~/.gitconfig 以及上面的conflicts 行:

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

    这会给你:

    $ 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
    

    【讨论】:

      【解决方案2】:

      git status显示自动合并失败和有冲突的文件,并提示如何记录这些文件的解析状态。

      【讨论】:

      • 是的,但是...是否有任何开关可以显示冲突文件?
      • @AlexeyShytikov,我不知道副手,但从手册来看,git status --short 似乎能够使用两个字符对列出的每个文件的状态进行编码——看看“短格式“ 部分。我认为可以在 shell 脚本中包装对 git status --short 的调用,该脚本将 sedgrep 仅输出相关位。
      • 另一方面,@me_and 给出的答案无论如何都需要编写脚本,因此采用他的方法可能更合乎逻辑(从而实现您自己的瓷器命令)。
      • 使用git status --porcelainsed 是我以前采用的方法,但git status 的操作比git ls-files 慢得多,因为前者需要将工作副本与索引。
      【解决方案3】:

      这是适用于 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
      

      【讨论】:

        【解决方案4】:

        TL;DR:

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

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



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

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

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


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

        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 在他的回答中所建议的,create an alias 也有帮助。

        【讨论】:

          猜你喜欢
          • 2016-01-12
          • 2011-11-12
          • 1970-01-01
          • 1970-01-01
          • 2022-06-23
          • 2015-09-23
          • 1970-01-01
          • 1970-01-01
          • 2014-02-16
          相关资源
          最近更新 更多