想要从“git diff”中排除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10415100/
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
Want to exclude file from "git diff"
提问by Michael
I am trying to exclude a file (db/irrelevant.php
) from a Git diff. I have tried putting a file in the db
subdirectory called .gitattributes
with the line irrelevant.php -diff
and I have also tried creating a file called .git/info/attributes
containing db/irrelevant.php
.
我正在尝试db/irrelevant.php
从 Git 差异中排除文件 ( )。我试图把在一个文件db
名为的子目录.gitattributes
与行irrelevant.php -diff
,我也尝试创建一个名为.git/info/attributes
包含db/irrelevant.php
。
In all cases, the db/irrelevant.php
file is included in the diff as a Git binary patch. What I want is for the changes to that file to be ignore by the diff command. What am I doing wrong?
在所有情况下,该db/irrelevant.php
文件都作为 Git 二进制补丁包含在差异中。我想要的是 diff 命令忽略对该文件的更改。我究竟做错了什么?
回答by Mr_and_Mrs_D
Omg, drivers and awk to exclude a lousy file ? Since git 1.9 somethingyou can:
天哪,驱动程序和 awk 来排除一个糟糕的文件?从git 1.9 开始,您可以:
git diff -- . ':(exclude)db/irrelevant.php' ':(exclude)db/irrelevant2.php'
Ah, elegance! See the quoted answer and for details this answerby @torek
啊,优雅!见引用答案和细节这个答案由@torek
回答by KurzedMetal
You could set up a custom diff driver with a no op command and assign it to those files that should be ignored.
您可以使用 no op 命令设置自定义差异驱动程序,并将其分配给那些应该被忽略的文件。
Create a repository specific diff driverwith this command
使用此命令创建存储库特定的差异驱动程序
git config diff.nodiff.command /bin/true
or for all your repos with --global
.
或为您所有的回购--global
。
(If /bin/true
doesn't exist in MacOS, alternatives would be using /usr/bin/true
or echo
).
(如果/bin/true
MacOS 中不存在,替代方案将使用/usr/bin/true
或echo
)。
Then, assign the new diff driver to those files you want ignoredin your .git/info/attributes
file.
然后,将新的差异驱动程序分配给您要在.git/info/attributes
文件中忽略的那些文件。
irrelevant.php diff=nodiff
If this state is supposed to be shared with other developers you could use .gitattributes
instead of .git/info/attributes
and share the git config
command with your peers (through a documentation file or something).
如果该状态应该与其他开发人员共享,您可以使用.gitattributes
而不是与您的同行.git/info/attributes
共享该git config
命令(通过文档文件或其他方式)。
回答by Szilárd Pfeiffer
You can also use filterdiffprogram of the patchutilsprogram collection to exclude some parts of a diff. For example:
您还可以使用patchutils程序集合的filterdiff程序来排除差异的某些部分。例如:
git diff | filterdiff -p 1 -x db/irrelevant.php
回答by Chiel ten Brinke
This method is shorter than the accepted answers.
这种方法比公认的答案要短。
git diff 987200fbfb 878cee40ba --stat -- ':!*.cs'
For more information about the different inclusion/exclusion possibilities read this other post
有关不同包含/排除可能性的更多信息,请阅读另一篇文章
回答by Ben Roux
This one-line solution requires no other utils/downloads:
这个单行解决方案不需要其他工具/下载:
git diff `git status -s |grep -v ^\ D |grep -v file/to/exclude.txt |cut -b4-`
Where file/to/exclude.txt
is the name of the file you would like to exclude, of course.
file/to/exclude.txt
当然,您要排除的文件的名称在哪里。
Edit: credit ksenzeefor fixing deleted files breaking the diff.
编辑:感谢 ksenzee修复了破坏差异的已删除文件。
回答by user3589608
Really good answer from KurzedMetalfor what I needed to do, which was ability to see that the file has changed, but not to generate the diff, which could have been huge, in my case.
KurzedMetal对我需要做的事情的回答非常好,即能够看到文件已更改,但不会生成差异,在我的情况下,差异可能很大。
But a colleague of mine suggested approach that was even simpler and worked for me:
但是我的一位同事建议的方法更简单并且对我有用:
adding .gitattributes
file in the directory where the file to be ignored by git diff
resides with the following content:
.gitattributes
在要忽略的文件所在的目录中添加文件,git diff
内容如下:
file-not-to-diff.bin -diff
file-not-to-diff.bin -diff
That still lets git status
"see" if the file changed. git diff
will also "see" that the file changed, but it will not generate the diff
.
这仍然可以让git status
“查看”文件是否更改。git diff
还将“看到”文件已更改,但不会生成diff
.
That .bin
extension for the file in the example was deliberate. I do realize that this is the default behavior of git for binary files, and it does not require special handling with .gitattributes
. But in my case, these files were recognized as text files by git and "file" utility and this did the trick.
这.bin
扩展了该示例中的文件是经过深思熟虑的。我确实意识到这是 git 对二进制文件的默认行为,并且不需要对.gitattributes
. 但在我的情况下,这些文件被 git 和“文件”实用程序识别为文本文件,这解决了问题。
回答by dlsso
Simplest answer
最简单的答案
git diff ':!db/irrelevant.php'
git diff ':!db/irrelevant.php'
It's just ':!<path to file>'
after your diff command. The diff command can be as complicated as you like, and you can use wildcards like *.min.js
or add multiple excludes (space separate the quoted blocks) if you want.
它就':!<path to file>'
在您的 diff 命令之后。diff 命令可以随您喜欢的复杂程度而定,如果需要,您可以使用通配符*.min.js
或添加多个排除项(空格分隔带引号的块)。
回答by jasonleonhard
Relative to the git root directory
相对于git根目录
git diff
accepts an optional exclude
git diff
接受一个可选的排除
git diff -- ":(exclude)thingToExclude"
You might want to add some wild cards
您可能想要添加一些通配符
git diff -- ":(exclude)*/thingToExclude/*"
Target specific file types
定位特定文件类型
git diff -- ":(exclude)*//*.png"
Or drop a little script for your dotfiles
或者为您的 dotfiles 删除一个小脚本
Such as .bash_profile
or .zshrc
比如.bash_profile
或.zshrc
gde() {
# git diff exclude files or folders
# usage:
# gde fileOrFolderNameToExclude
git diff -- ":(exclude)*//*"
}
回答by Nieznany Anonimiec
It's very simple, you can exclude what you want using standard unix commands, those commands are available under git bash even under windows environment. Below example shows how to exclude diff for pom.xml files, first check diff to master only for filenames, then using 'grep -v' exclude files which you don't want and then run again diff to master with prepapred list:
很简单,你可以使用标准的unix命令排除你想要的东西,这些命令即使在windows环境下也可以在git bash下使用。下面的示例显示了如何排除 pom.xml 文件的差异,首先检查 diff 到 master 只针对文件名,然后使用 'grep -v' 排除您不想要的文件,然后再次运行 diff to master 和 prepapred 列表:
git diff master `git diff --name-only master | grep -v pom.xml`
回答by BlueMagma
I do the following:
我执行以下操作:
git add *pattern_to_exclude*
git diff
git reset HEAD .
I know it's not an elegant solution and it sometimes canot be used (eg. if you already have stuff staged) , but it does the trick without having to type a complicated command
我知道这不是一个优雅的解决方案,有时无法使用它(例如,如果你已经有东西上演),但它不需要输入复杂的命令就可以解决问题