简体   繁体   English

如何根据文件扩展名过滤git diff?

[英]How to filter git diff based on file extensions?

是否可以选择将git diff限制为一组给定的文件扩展名?

是的,如果你确保 git 扩展一个 glob 而不是你的 shell,那么它会在任何级别匹配,所以这样的东西(引号很重要)应该可以正常工作。

git diff -- '*.c' '*.h'

要递归包含文件(包括当前目录),这对我有用:

git diff -- '***.py'

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 . 2 启用 globstar 时, git diff -- **/*.py已经包含./*.py In Bash's manpage: 'If followed by a /, two adjacent *s will match only directories and subdirectories.'在 Bash 的联机帮助页中:“如果后跟 /,则两个相邻的 *s 将仅匹配目录和子目录。”

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

As tested on git version 2.18.0, the file extension should be quoted with double quotes.在 git version 2.18.0 上测试,文件扩展名应该用双引号引起来。 If you want to find the last differences between your local repository and the remote one, after pulling, you can use:如果您想找到本地存储库和远程存储库之间的最后一个差异,在拉取后,您可以使用:

git diff YourBranchName@{1} YourBranchName --name-only "*.YourFileExtionsion"

For example:例如:

git diff master@{1} origin/master --name-only "*.cs"

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 --

None of the answers above seem to work for me under git bash on Windows.在 Windows 上的git bash下,上面的答案似乎都不适合我。 I am not sure if it is a version thing (I'm using 1.8.4) or Windows/bash thing;我不确定它是版本问题(我使用的是 1.8.4)还是 Windows/bash 问题; 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).另外,在我的情况下,我想区分两个分支,其中每个分支都有另一个分支中不存在的其他文件(因此基于“查找”的文件是疏忽的)。

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`

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 .我发现这个问题是因为我想从git diff排除.info文件。 I achieved this by staging it with git add *.info , which reduces the files left.我通过使用git add *.info暂存它来实现这一点,这减少了剩下的文件。

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结尾时,才会显示指定提交的差异,根据您的情况修改管道。

Attention that params order makes difference...for example:注意 params 顺序会有所不同......例如:

git diff master --name-only --relative -- "**/*.ts" "**/*.tsx" "**/*.js" "**/*.jsx" "**/*.vue"

'diff' need to be followed with 'master' 'diff' 需要跟在 'master' 后面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM