我可以使用像“system <<-”这样的引号从谷歌中搜索精确匹配。

我如何为GitHub做同样的事情?


当前回答

(在2022年之前)你不能。GitHub官方搜索规则:

Due to the complexity of searching code, there are a few restrictions on how searches are performed: Only the default branch is considered. In most cases, this will be the master branch. Only files smaller than 384 KB are searchable. Only repositories with fewer than 500,000 files are searchable. You must always include at least one search term when searching source code. For example, searching for language:go is not valid, while amazing language:go is. At most, search results can show two fragments from the same file, but there may be more results within the file. You can't use the following wildcard characters as part of your search query: . , : ; / \ ` ' " = * ! ? # $ & + ^ | ~ < > ( ) { } [ ] The search will simply ignore these symbols.

更新:GitHub现在支持文字字符串,但你也可以尝试下面一些更强大的方法。


尝试Sourcegraph

对于支持正则表达式的复杂搜索,请尝试Sourcegraph。


克隆并使用git-grep:

Git支持使用Git -grep命令在源代码中搜索。只需克隆一个存储库,并在文件夹中使用命令:

git grep "text-to-search"

选择:

我推荐你试试ripgrep工具,它快速简单。类似于git-grep,但看起来更好:

rg "text-to-search"

你可以使用标准的grep来搜索文件中的任何文本:

grep -r "text-to-search" /repository

其他回答

您可以直接使用谷歌。

这个怎么样?

"your_string_to_search" site::https://github.com
"your_string_to_search" site::https://gist.github.com

如果你的包在debian中,你可以使用他们的代码搜索,它支持正则表达式:https://codesearch.debian.net/

从2021年2月11日起,你可以在搜索字符串周围加上引号

没有引用:

报价:

虽然现在可以搜索精确的字符串,但该功能还不支持对特殊字符的搜索。例子:

(在2022年之前)你不能。GitHub官方搜索规则:

Due to the complexity of searching code, there are a few restrictions on how searches are performed: Only the default branch is considered. In most cases, this will be the master branch. Only files smaller than 384 KB are searchable. Only repositories with fewer than 500,000 files are searchable. You must always include at least one search term when searching source code. For example, searching for language:go is not valid, while amazing language:go is. At most, search results can show two fragments from the same file, but there may be more results within the file. You can't use the following wildcard characters as part of your search query: . , : ; / \ ` ' " = * ! ? # $ & + ^ | ~ < > ( ) { } [ ] The search will simply ignore these symbols.

更新:GitHub现在支持文字字符串,但你也可以尝试下面一些更强大的方法。


尝试Sourcegraph

对于支持正则表达式的复杂搜索,请尝试Sourcegraph。


克隆并使用git-grep:

Git支持使用Git -grep命令在源代码中搜索。只需克隆一个存储库,并在文件夹中使用命令:

git grep "text-to-search"

选择:

我推荐你试试ripgrep工具,它快速简单。类似于git-grep,但看起来更好:

rg "text-to-search"

你可以使用标准的grep来搜索文件中的任何文本:

grep -r "text-to-search" /repository

今天我试图在Github上的任何repo中寻找名为logback.xml的文件中的过滤器类的精确匹配。于是我提出了下面这个问题。

"filter class" in:file filename:logback.xml

要启用与引号的精确匹配,您需要在搜索后使用“in:file”修饰符。匹配不是很精确,单词“class”必须跟在单词“filter”后面,但似乎两个单词之间可以有0个或更多的空格或符号字符。