想要从“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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:35:27  来源:igfitidea点击:

Want to exclude file from "git diff"

gitdiffgit-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 dbsubdirectory called .gitattributeswith the line irrelevant.php -diffand I have also tried creating a file called .git/info/attributescontaining db/irrelevant.php.

我正在尝试db/irrelevant.php从 Git 差异中排除文件 ( )。我试图把在一个文件db名为的子目录.gitattributes与行irrelevant.php -diff,我也尝试创建一个名为.git/info/attributes包含db/irrelevant.php

In all cases, the db/irrelevant.phpfile 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/truedoesn't exist in MacOS, alternatives would be using /usr/bin/trueor echo).

(如果/bin/trueMacOS 中不存在,替代方案将使用/usr/bin/trueecho)。

Then, assign the new diff driver to those files you want ignoredin your .git/info/attributesfile.

然后,将新的差异驱动程序分配给您要.git/info/attributes文件中忽略的那些文件。

irrelevant.php    diff=nodiff

If this state is supposed to be shared with other developers you could use .gitattributesinstead of .git/info/attributesand share the git configcommand 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.txtis 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 .gitattributesfile in the directory where the file to be ignored by git diffresides 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 diffwill also "see" that the file changed, but it will not generate the diff.

这仍然可以让git status“查看”文件是否更改。git diff还将“看到”文件已更改,但不会生成diff.

That .binextension 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.jsor 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 diffaccepts 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_profileor .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

我知道这不是一个优雅的解决方案,有时无法使用它(例如,如果你已经有东西上演),但它不需要输入复杂的命令就可以解决问题