bash 按文件名过滤的 git diff
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15438915/
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
git diff filtered by file name
提问by Zantier
I would like the results of git diffto be filtered by the file name.
我希望git diff按文件名过滤结果。
In particular, I want a diff for all of the files named "AssemblyInfo.cs", but located anywhere within the git repository.
特别是,我想要所有名为“AssemblyInfo.cs”的文件的差异,但位于 git 存储库中的任何位置。
I am using git on Cygwin, if that makes a difference.
我在 Cygwin 上使用 git,如果这有区别的话。
采纳答案by GoZoner
File arguments to git diffneed to be delimited by --- try this:
git diff需要分隔的文件参数--- 试试这个:
find . -name <pattern> | xargs git diff --
xargs makes sure spaces, tabs, newlines, etc are handled correctly.
xargs 确保正确处理空格、制表符、换行符等。
You could debug it with the --name-statusargument to git diff. You could also try:
你可以用--name-status参数调试它git diff。你也可以试试:
git diff --name-only | grep <pattern>
[edit] Try:
[编辑] 尝试:
git diff --name-status -- `find . -name '<pattern>'`
ebg@taiyo(98)$ git diff --name-status -- `find . -name '*.scm'`
M scheme/base/boolean.scm
M surf/compiler/common.scm
M surf/compiler/compile.scm
M surf/compiler/expand.scm
回答by AggieBlue
The simplest method is to simply use a wildcard:
最简单的方法是简单地使用通配符:
git diff -- '*AssemblyInfo.cs'
At least this works on my Git v1.8.4, bash 3.2, and zsh 5.7.
至少这适用于我的 Git v1.8.4、bash 3.2 和 zsh 5.7。
回答by rkwatra
Probably the simplest option is to use:
可能最简单的选择是使用:
git diff "*/*AssemlyInfo.cs"
works as of git 2.20.1
从 git 2.20.1 开始工作
回答by shampoo
find . -iregex AssemblyInfo\.cs -exec git diff {} +
you can replace the AssemblyInfo\.cswith your regex.
你可以AssemblyInfo\.cs用你的正则表达式替换。
回答by siegi
While the answer given by GoZonerworks for some (hundred?) files, it executes git diffmultiple times (in the case of xargs) or fails (in the case of git diff … -- `find …`) if there is a large number of files to diff. This might be a problem or not, depending on your use case.
虽然GoZoner 给出的答案适用于某些(数百个?)文件,但如果有大量文件要git diff区分,它会执行多次(在 的情况下xargs)或失败(在 的情况下git diff … -- `find …`)。这可能是一个问题,也可能不是问题,具体取决于您的用例。
A possible solution is to create a commit containing only changes to files of interest and diff the commits. Based on an answer on unstaging files matching some patternI came up with this solution:
一个可能的解决方案是创建一个提交,只包含对感兴趣的文件的更改并比较提交。基于对匹配某种模式的取消暂存文件的回答,我想出了这个解决方案:
git co <new_branch> --detach
git reset --soft <old_branch>
Now git status --porcelain | grep <pattern>shows all files that should be compared. All other files can be listed by passing -vto grep, i.e. git status --porcelain | grep -v <pattern>. This files need to be reset to the state of <old_branch>:
现在git status --porcelain | grep <pattern>显示应该比较的所有文件。所有其他文件都可以通过传递-v到grep,即git status --porcelain | grep -v <pattern>. 此文件需要重置为以下状态<old_branch>:
# Remove the destination of renamed and copied files not matching <pattern>
git status --porcelain | grep -v <pattern> | grep '^R \|^C ' | sed 's/.* -> //' | xargs git rm -f --
# Remove added files not matching <pattern>
git status --porcelain | grep -v <pattern> | grep '^A ' | cut -c 4- | xargs git rm -f --
# Restore deleted files not matching <pattern>
git status --porcelain | grep -v <pattern> | grep '^M \|^D ' | cut -c 4- | xargs git checkout HEAD --
(Note that using xargsis not a problem in this case, as calling git rmand git checkoutmultiple times is ok.)
(请注意,xargs在这种情况下使用不是问题,因为调用git rm和git checkout多次是可以的。)
Now the index (and working copy) only contains changes to the files matched by <pattern>. The next thing to do is to commit this changes:
现在索引(和工作副本)只包含对匹配的文件的更改<pattern>。接下来要做的是提交此更改:
git commit -m "Changes between <old_branch> and <new_branch> in files matching <pattern>"
That's it! Now we can use git diffas usual:
就是这样!现在我们可以git diff像往常一样使用:
git diff HEAD^..HEAD
You can use all options or arguments you like.
您可以使用所有您喜欢的选项或参数。
Note: This solution is not tested extensively and may fail e.g. on files with special characters or other special cases… Suggestions to improve the solution are welcome ;-)
注意:此解决方案未经过广泛测试,可能会失败,例如处理带有特殊字符或其他特殊情况的文件……欢迎提出改进解决方案的建议;-)

