在所有 Git 分支中搜索文件

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

Searching for a file in all Git branches

gitgrep

提问by bgcode

How do I search all Git branches of a project for a file name?

如何在项目的所有 Git 分支中搜索文件名?

I remember part of the filename (just the ending), so I'd like to be able to search for something like *_robot.phpacross all branches, and see which files match that. I'd preferably like to have it search history, and not just the HEADs of branches.

我记得文件名的一部分(只是结尾),所以我希望能够*_robot.php在所有分支中搜索类似的内容,并查看哪些文件与之匹配。我更希望拥有它的搜索历史,而不仅仅是分支的 HEAD。

回答by manojlds

This is one way:

这是一种方式:

git log --all --name-only --pretty=format: | sort -u | grep _robot.php

回答by Eric O Lebigot

Here is a simpler variation on @manojlds's solution: Git can indeed directly consider all the branches (--all), print the names of their modified files (--name-only), and only these names (--pretty=format:).

这是@manojlds 解决方案的一个更简单的变体:Git 确实可以直接考虑所有分支 ( --all),打印其修改文件的名称 ( --name-only),并且仅打印这些名称 ( --pretty=format:)。

But Git can also first filter certain file names (regular expression) by putting the file name regular expression after the --separator:

但是 Git 也可以通过将文件名正则表达式放在--分隔符之后来先过滤某些文件名(正则表达式):

git log --all --name-only --pretty=format: -- <file_name_regexp> | sort -u

So, you can directly do:

因此,您可以直接执行以下操作:

git log --all --name-only --pretty=format: -- _robot.php | sort -u