git diff --stat 排除某些文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39931781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:20:01  来源:igfitidea点击:

git diff --stat exclude certain files

gitgit-diff

提问by Mr_and_Mrs_D

Trying to adapt the answers from Want to exclude file from "git diff"for the --statflag and failing - the accepted answer(create a driver) seems unix only (redirect to /bin/true, whatever this means) plus it creates a driver and assigns it to the file kind of permanently, while I am looking for a switch to temporarily disable the diff for a file (or rather some files).

试图调整来自Want to exclude file from "git diff"for the --statflag 和失败的答案-接受的答案(创建驱动程序)似乎只是 unix(重定向到 /bin/true,无论这意味着什么)加上它创建一个驱动程序和将它永久分配给文件类型,而我正在寻找一个开关来临时禁用文件(或某些文件)的差异。

The scripting solution:

脚本解决方案

git diff `git status -s |grep -v ^\ D |grep -v file/to/exclude.txt |cut -b4-`

actually calls git status and edits its output - while what I want is to instruct git diff itself to ignore some files while calculating the (simple) --stat (just lines changed). I went through git-diffdocs but can't seem to find such an option. Anyone give me a hand ?

实际上调用 git status 并编辑其输出 - 而我想要的是指示 git diff 本身在计算(简单)--stat(只是行更改)时忽略一些文件。我浏览了git-diff文档,但似乎找不到这样的选项。有人帮我吗?

$ git --version
git version 2.6.1.windows.1

回答by torek

The exclude pathspec trick, described in Making 'git log' ignore changes for certain paths, works here:

排除路径规范技巧,在使 'git log' 忽略某些路径的更改中描述,在这里工作:

git diff --stat -- . ':(exclude)file/to/exclude.txt'

or, if you are in a subdirectory:

或者,如果您在子目录中:

git diff --stat -- :/ ':(exclude,top)file/to/exclude.txt'

The latter can be spelled in various ways. For instance, this also works:

后者可以用多种方式拼写。例如,这也有效:

git diff --stat ':(top)' :!/file/to/exclude.txt

as does:

就像:

git diff --stat :/: :!/:file/to/exclude.txt

These are described in the gitglossarydocumentationunder the "pathspec" section. Note that the exclude feature is new in Git version 1.9 (and slightly broken until 1.9.2). The leading /is an alias for topand the !is an alias for exclude, with the long forms requiring the parentheses. The trailing colon before the actual pathname is optional when using the single-character aliases but forbidden when using the parentheses (this rule trips me up every time—I keep wanting to use :(exclude):...rather than :(exclude)...). The single quotes around the (top)and (exclude)pathspec components above are to protect the parentheses from being interpreted by the (Unix/Linux) shells; the Windows shell may have different ideas about which characters need protection.

这些在“pathspec”部分下gitglossary文档中进行了描述。请注意,排除功能是 Git 1.9 版中的新功能(在 1.9.2 之前略有损坏)。前导/是 for 的别名top,the!是 的别名exclude,长格式需要括号。在使用单字符别名时,实际路径名之前的尾随冒号是可选的,但在使用括号时被禁止(这条规则每次都让我感到困惑——我一直想使用:(exclude):...而不是:(exclude)...)。(top)和周围的单引号(exclude)上面的 pathspec 组件是为了保护括号不被 (Unix/Linux) shell 解释;Windows shell 可能对哪些字符需要保护有不同的想法。