从 git diff 中排除目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4380945/
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 09:36:50  来源:igfitidea点击:

Exclude a directory from git diff

git

提问by Mysrt

I'm wondering how I can exclude an entire directory from my Git diff. (In this case /spec). I'm creating a diff for our entire software release using the git diff command. However the changes to the specs are irrelevant for this procedure, and just create headaches. now I know i can do

我想知道如何从我的 Git 差异中排除整个目录。(在本例中为 /spec)。我正在使用 git diff 命令为我们的整个软件版本创建一个差异。然而,规范的更改与此过程无关,只会造成麻烦。现在我知道我可以做到

git diff previous_release..current_release app/

This would create a diff for all the changes in the app directory, but not for instance, in the lib/ directory. Does anyone know how to accomplish this task? Thanks!

这将为 app 目录中的所有更改创建一个差异,但不会在 lib/ 目录中创建差异。有谁知道如何完成这项任务?谢谢!

Edit: I just want to be clear, I know I can just string the parameters of all of my directories on the end, minus /spec. I was hoping there was a way to truly exclude a single directory in the command.

编辑:我只想说清楚,我知道我可以将所有目录的参数串到最后,减去 /spec。我希望有一种方法可以真正排除命令中的单个目录。

采纳答案by Cascabel

Assuming you use bash, and you've enabled extended globbing (shopt -s extglob), you could handle that from the shell side:

假设您使用 bash,并且您启用了扩展的 globbing ( shopt -s extglob),您可以从 shell 端处理它:

git diff previous_release current_release !(spec)

Saves you having to list all other things.

省去您列出所有其他事情的麻烦。

Or, shell-agnostic:

或者,外壳不可知:

git diff previous_release current_release --name-only | grep -v '^spec/' \
    | xargs git diff previous_release current_release --

You could wrap that up in a one-liner shell script to save yourself having to retype the arguments.

您可以将其封装在一个单行 shell 脚本中,这样您就不必重新键入参数。

回答by cdleonard

Based on this answeryou can also use:

基于此答案,您还可以使用:

git diff previous_release..current_release -- . ':!spec'

This is a newish git feature which allows excluding certain paths. It should be more reliable than various shell oneliners.

这是一个新的 git 功能,它允许排除某些路径。它应该比各种shell oneliners更可靠。

I'm posting this here because this question is still the #1 hit for "git diff exclude path".

我在这里发布这个是因为这个问题仍然是“git diff exclude path”的第一个问题。

回答by David Graham

If you want to specify more than one path to exclude in a git diff, you just add additional exclusion parameters to the end, e.g. to exclude everything in vendorand bindirectories from the stats:-

如果您想在 git diff 中指定多个要排除的路径,您只需在最后添加额外的排除参数,例如从统计信息中排除vendorbin目录中的所有内容:-

git diff --stat previous_release..current_release -- . ':!vendor' ':!bin'

回答by VonC

You can try and unset the diff attribute for any files within the lib directory.
.gitattributes:

您可以尝试取消设置 lib 目录中任何文件的 diff 属性。
.gitattributes

lib/* -diff


racl101adds in the comments:

racl101在评论中补充道:

Just to add to this, for those of you who want to limit the git diffoutput of files of a specific type, within a specific directory and its subdirectories, like all the JavaScript generated by Webpack's bundling of your .jsfiles, for example, you can add this to .gitattributes:

只是为了补充一点,对于那些想要限制git diff特定类型文件输出的人,在特定目录及其子目录中,例如 Webpack 捆绑.js文件生成的所有 JavaScript ,您可以添加这个到.gitattributes

dist/js/**/*.js -diff

Then you don't see all that noise in your git diff output and it just shows as: Binary files ... differwhich is more helpful.

然后你不会在你的 git diff 输出中看到所有的噪音,它只是显示为:Binary files ... differ这更有帮助。

回答by MaxPRafferty

Git diff now accepts a custom exclusion format: git diff -- ':(exclude)lib/*'

Git diff 现在接受自定义排除格式: git diff -- ':(exclude)lib/*'

Be careful if you have a lot of repetitive folder names ('/app', '/lib', etc.), as this will exclude files relative to the current working directory AND the git root directory.

如果您有很多重复的文件夹名称('/app'、'/lib' 等),请小心,因为这将排除与当前工作目录和 git 根目录相关的文件。

回答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"

Some syntactical sugar applied

应用了一些语法糖

git diff -- ":!/$1/"

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 -- ":!/$1/"
}

回答by Jed Schneider

git diff app lib