从 git-diff 中排除文件

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

Excluding files from git-diff

git

提问by zoul

I am tracking a project with git. There are some Xcode project files in the working copy that I want to keep tracking, but do not want to see in diffs, because there are always dozens of changed lines that I am never interested in. Is there a simple way to have git-diffskip these files? I've tried to set up a custom “silent” diff tool:

我正在用 git 跟踪一个项目。工作副本中有一些 Xcode 项目文件我想继续跟踪,但不想在差异中看到,因为总是有几十行我从来不感兴趣。有没有一种简单的方法可以git-diff跳过这些文件?我尝试设置一个自定义的“无声”差异工具:

$ cat .gitattributes 
Project.xcodeproj/* diff=nodiff

$ cat ~/.gitconfig 
[diff "nodiff"]
    command = /bin/true

But:

但:

$ git diff
external diff died, stopping at Project.xcodeproj/zoul.mode1v3.

What am I doing wrong?

我究竟做错了什么?

回答by CB Bailey

The problem is that /bin/truewill return immediately without reading its input. git difftherefore thinks, quite reasonably, that it has died prematurely.

问题是它/bin/true会立即返回而不读取其输入。git diff因此,相当合理地认为它已经过早死亡。

What you really want to do is to unset the diff attribute, not set it to a bogus command. Try this in your .gitattributes:

您真正想要做的是取消设置 diff 属性,而不是将其设置为虚假命令。在您的.gitattributes

Project.xcodeproj/* -diff

回答by yanjost

You may use an alias in your .git/config

您可以在 .git/config 中使用别名

[alias]
        mydiff = !git diff | filterdiff -x "*/Project.xcodeproj/*"

You need filterdiff (from patchutils) for this trick.

你需要 filterdiff(来自 patchutils)来实现这个技巧。

sudo apt-get install patchutils

Still the diff isn't perfect, it leaves some garbage :

差异仍然不完美,它留下了一些垃圾:

yannick@yannick-desktop:~/git-filter-test$ git mydiff
diff --git a/Project.xcodeproj/dummy.txt b/Project.xcodeproj/dummy.txt
index 3e1f9e6..89dfed9 100644
diff --git a/dummy2.txt b/dummy2.txt
index 91966ce..d9588a9 100644
--- a/titi.txt
+++ b/titi.txt
@@ -1,3 +1,3 @@
 aaaaaaaaaa
-bbbbbbbbb
 cccccc
+ddd

回答by Connor McKay

Another solution that produces clean output without any external tools (add to .git/config):

另一种无需任何外部工具即可产生干净输出的解决方案(添加到.git/config):

[alias]
    mydiff = !git diff -- $(git diff --name-only | grep -Ev "Project.xcodeproj/")

Then run it with:

然后运行它:

git mydiff

Note that git diff --name-onlyis better than git ls-filesbecause it will pass a shorter list of files to git diff, since only files that are modified will be included. I've run into trouble with exceeding the maximum number of arguments in large projects when using git ls-files.

请注意,这git diff --name-onlygit ls-files将更短的文件列表传递给更好git diff,因为只会包含修改过的文件。使用git ls-files.

回答by Evolve

Just incase someone else has the same pain we had. We wanted to exclude a file that had already been committed.

以防万一其他人有和我们一样的痛苦。我们想排除一个已经提交的文件。

This post was way more useful: working with .git/info/exclude too late

这篇文章更有用: 使用 .git/info/exclude 太晚了

Specifically what you need to ignore a file is actually use the command git remove See git rm(http://www.kernel.org/pub/software/scm/git/docs/git-rm.html)

具体来说,您需要忽略文件实际上是使用命令 git remove 参见git rm( http://www.kernel.org/pub/software/scm/git/docs/git-rm.html)

you test it by going

你去测试一下

git rm --dry-run *.log
(if you say wanted to exclude all the log files)

git rm --dry-run *.log
(如果您说要排除所有日志文件)

this will output what would beexcluded if you ran it.

如果您运行它,这将输出将被排除的内容。

then

然后

you run it by going

你去运行它

git rm *.log
(or whatever filename path / expression you want to)

git rm *.log
(或您想要的任何文件名路径/表达式)