我只需要一份冲突文件列表。

还有什么比:

git ls-files -u  | cut -f 2 | sort -u

or:

git ls-files -u  | awk '{print $4}' | sort | uniq

我想我可以为此设置一个方便的别名,但我想知道专业人士是如何做到的。我会用它来写shell循环,比如自动解决冲突等等。也许可以通过插入mergetool.cmd来替换这个循环?


当前回答

只需阅读手册页的git状态,并过滤什么是需要的。

 git status --help

代码片段:

       For paths with merge conflicts, X and Y show the modification states of each side of the merge. For paths that do not have merge conflicts, X shows the status of the index, and Y shows the status of the work tree. For untracked
   paths, XY are ??. Other status codes can be interpreted as follows:

   •   ' ' = unmodified

   •   M = modified

   •   A = added

   •   D = deleted

   •   R = renamed

   •   C = copied

   •   U = updated but unmerged

   Ignored files are not listed, unless --ignored option is in effect, in which case XY are !!.

       X          Y     Meaning
       -------------------------------------------------
                [AMD]   not updated
       M        [ MD]   updated in index
       A        [ MD]   added to index
       D                deleted from index
       R        [ MD]   renamed in index
       C        [ MD]   copied in index
       [MARC]           index and work tree matches
       [ MARC]     M    work tree changed since index
       [ MARC]     D    deleted in work tree
       [ D]        R    renamed in work tree
       [ D]        C    copied in work tree
       -------------------------------------------------
       D           D    unmerged, both deleted
       A           U    unmerged, added by us
       U           D    unmerged, deleted by them
       U           A    unmerged, added by them
       D           U    unmerged, deleted by us
       A           A    unmerged, both added
       U           U    unmerged, both modified
       -------------------------------------------------
       ?           ?    untracked
       !           !    ignored
       -------------------------------------------------

其他回答

假设你知道git根目录${GIT_ROOT}在哪里,你可以这样做,

 cat ${GIT_ROOT}/.git/MERGE_MSG | sed '1,/Conflicts/d'

Git的状态会在有冲突的文件旁边显示“已修改”,而不是“已修改”或“新文件”等

正如在其他答案中突出显示的那样,我们可以简单地使用命令git status,然后查找未合并路径下列出的文件:

我一直只使用git状态。

可以在最后添加awk来获得文件名吗

git status -s | grep ^U | awk '{print $2}'

Jones Agyemang的回答对于大多数用例来说可能已经足够了,并且是我的解决方案的一个很好的起点。对于在Git Bent(我制作的Git包装器库)中编写脚本,我需要一些更健壮的东西。我正在发布我所编写的原型,它还不是完全脚本友好的

笔记

链接的答案检查<<<<<<< HEAD,它不工作的合并冲突使用git stash应用程序,有<<<<<<<更新的上游 我的解决方案确认了=======和>>>>>>>的存在 链接的答案肯定更有性能,因为它不需要做那么多 我的解决方案不提供行号

打印合并冲突的文件

您需要下面的str_split_line函数。

# Root git directory
dir="$(git rev-parse --show-toplevel)"
# Put the grep output into an array (see below)
str_split_line "$(grep -r "^<<<<<<< " "${dir})" files
bn="$(basename "${dir}")"
for i in "${files[@]}"; do 
    # Remove the matched string, so we're left with the file name  
    file="$(sed -e "s/:<<<<<<< .*//" <<< "${i}")"

    # Remove the path, keep the project dir's name  
    fileShort="${file#"${dir}"}"
    fileShort="${bn}${fileShort}"

    # Confirm merge divider & closer are present
    c1=$(grep -c "^=======" "${file}")
    c2=$(grep -c "^>>>>>>> " "${file}")
    if [[ c1 -gt 0 && c2 -gt 0 ]]; then
        echo "${fileShort} has a merge conflict"
    fi
done

输出

projectdir/file-name
projectdir/subdir/file-name

按行函数拆分字符串

如果你不想把它作为一个单独的函数,你可以复制这段代码

function str_split_line(){
# for IFS, see https://stackoverflow.com/questions/16831429/when-setting-ifs-to-split-on-newlines-why-is-it-necessary-to-include-a-backspac
IFS="
"
    declare -n lines=$2
    while read line; do
        lines+=("${line}")
    done <<< "${1}"
}