我试图从Git diff中排除一个文件(db/ irrelevance .php),我已经尝试将一个文件放在db子目录中,名为.gitattributes,并使用行irrelevance .php -diff 我还尝试创建了一个名为.git/info/attributes的文件,其中包含db/无关的。php。

在所有情况下,db/ irrelevance .php文件都作为Git二进制补丁包含在diff中。我想要的是由diff命令忽略对该文件的更改。我做错了什么?


当前回答

天哪,驱动程序和awk排除一个糟糕的文件?自从git 1.9以来,你可以:

git diff -- . ':(exclude)db/irrelevant.php' ':(exclude)db/irrelevant2.php'

(在Windows上,将单引号'替换为双引号'。)

啊,优雅!请参阅引用的答案和@torek的回答

其他回答

git diff remote/master..master --name-only -- ':!README.rst'

给我(git版本2.11.0)

fatal: There is nothing to exclude from by :(exclude) patterns.
Perhaps you forgot to add either ':/' or '.' ?
git diff remote/master..master --name-only -- './*' ':!README.rst'

工作得很好,来源:https://stackoverflow.com/a/30084612/5155484

天哪,驱动程序和awk排除一个糟糕的文件?自从git 1.9以来,你可以:

git diff -- . ':(exclude)db/irrelevant.php' ':(exclude)db/irrelevant2.php'

(在Windows上,将单引号'替换为双引号'。)

啊,优雅!请参阅引用的答案和@torek的回答

相对于git根目录

Git diff接受一个可选的排除

git diff -- ":(exclude)thingToExclude"

您可能需要添加一些通配符

git diff -- ":(exclude)*/thingToExclude/*"

目标特定的文件类型

git diff -- ":(exclude)*/$1/*.png"

或者为您的dotfiles添加一个小脚本

例如.bash_profile或.zshrc

gde() {
    # git diff exclude files or folders 
    # usage: 
    # gde fileOrFolderNameToExclude
    git diff -- ":(exclude)*/$1/*"
}

类似于本·鲁克斯的解决方案,但无论如何都是共享的:

git status --porcelain | grep -v $PATTERN_TO_EXCLUDE | awk '{print $2}' | xargs git diff

或者,如果更改已经在本地提交:

git diff --name-only origin/master | grep -v $PATTERN_TO_EXCLUDE | xargs git diff origin/master

例子:

git status --porcelain | grep -v docs | awk '{print $2}' | xargs git diff origin/master

git diff --name-only origin/master | grep -v docs | xargs git diff origin/master

这个单行解决方案不需要其他utils/downloads:

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

当然,file/to/exclude.txt是您想要排除的文件的名称。

编辑:credit ksenzee修复被删除的文件打破差异。