git 如何根据文件扩展名过滤git diff?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8554776/
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
How to filter git diff based on file extensions?
提问by vfclists
Is there an option to restrict git diff
to a given set of file extensions?
是否可以选择限制git diff
给定的一组文件扩展名?
回答by CB Bailey
Yes, if you ensure that git expands a glob rather than your shell then it will match at any level so something like this (quotes are important) should work fine.
是的,如果你确保 git 扩展一个 glob 而不是你的 shell,那么它会在任何级别匹配,所以这样的东西(引号很重要)应该可以正常工作。
git diff -- '*.c' '*.h'
回答by Bohumir Zamecnik
To include files recursively (including current dir) this worked for me:
要递归包含文件(包括当前目录),这对我有用:
git diff -- '***.py'
回答by sehe
Either use your shell's globstar (which does a recursive search)1,2:
要么使用你的 shell 的 globstar (它进行递归搜索)1 ,2 :
shopt -s globstar
git diff -- *.py **/*.py
or use find:
或使用查找:
find -name '*.py' -print0 | xargs -0 git diff --
Both of these are special-names and whitespace proof. Although you might want to filter for directories having the .py extension :)
这两个都是特殊名称和空格证明。虽然您可能想要过滤具有 .py 扩展名的目录:)
1 I like to do git diff -- {.,**}/*.py
usually
1 我git diff -- {.,**}/*.py
平时喜欢做的
2 When globstar is enabled, git diff -- **/*.py
already includes ./*.py
. In Bash's manpage: 'If followed by a /, two adjacent *s will match only directories and subdirectories.'
2 启用 globstar 时,git diff -- **/*.py
已包含./*.py
. 在 Bash 的联机帮助页中:“如果后跟 /,则两个相邻的 *s 将仅匹配目录和子目录。”
回答by hughdbrown
Command line argument for extension.
扩展的命令行参数。
git diff *.py
In the alternative, you can pipe find
into git diff
:
或者,您可以通过管道find
输入git diff
:
find . -name '*.py' -type f | git diff --
回答by Mat
For simple file patterns, this seems to work:
对于简单的文件模式,这似乎有效:
$ git ls-files -zm '*.txt' | xargs --null git diff
Whitespace safe, and you can have multiple extensions too:
空白安全,你也可以有多个扩展:
$ git ls-files -zm '*.h|*.c|*.cpp' | xargs --null git diff
回答by marjan.javid
As tested on git version 2.18.0, the file extension should be quoted with double quotes. If you want to find the last differences between your local repository and the remote one, after pulling, you can use:
在 git version 2.18.0 上测试,文件扩展名应该用双引号引起来。如果您想找到本地存储库和远程存储库之间的最后一个差异,在拉取后,您可以使用:
git diff YourBranchName@{1} YourBranchName --name-only "*.YourFileExtionsion"
For example:
例如:
git diff master@{1} origin/master --name-only "*.cs"
回答by Yonatan
None of the answers above seem to work for me under git bash
on Windows. I am not sure if it is a version thing (I'm using 1.8.4) or Windows/bash thing; also, in my case, I wanted to diff two branches where each branch had additional files not present in the other branch (thus the 'find' based ones are remiss).
git bash
在 Windows下,上面的答案似乎都不适合我。我不确定它是版本问题(我使用的是 1.8.4)还是 Windows/bash 问题;另外,在我的情况下,我想区分两个分支,其中每个分支都有另一个分支中不存在的附加文件(因此基于“查找”的文件是疏忽的)。
Anyway this worked for me (in my example, looking for a diff between python files):
无论如何,这对我有用(在我的示例中,寻找 python 文件之间的差异):
git diff branch1 branch2 -- `git diff --summary branch1 branch2 | egrep '\.py$' | cut -d ' ' -f 5`
回答by Alan Whitelaw
git diff
will only show differences in unstaged files.
git diff
只会显示未暂存文件的差异。
I found this question because I wanted to exclude .info
files from git diff
. I achieved this by staging it with git add *.info
, which reduces the files left.
我发现这个问题是因为我想.info
从git diff
. 我通过使用 暂存它来实现这一点git add *.info
,这减少了剩下的文件。
回答by MatrixManAtYrService
I wound up with this:
我结束了这个:
commit=<the_commit_hash_goes_here> && git diff --name-only $commit | grep -i Test | egrep -v '\.sql$' | xargs git diff $commit --
This shows diffs for the specified commit only if the filename contains the word 'test' (case insensitive) and does not end with .sql
, modify the pipeline as necessary for your case.
仅当文件名包含单词 'test'(不区分大小写)且不以.sql
, 根据您的情况修改管道时,才会显示指定提交的差异。