我可以使用像“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

其他回答

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

没有引用:

报价:

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

您可以直接使用谷歌。

这个怎么样?

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

(在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的代码搜索返回该子字符串的实例的部分解决方案是:(1)用空格替换搜索词中的标点符号字符,(2)将搜索词括在引号中。

例如,不要使用搜索词:

回购:my_repo my_image_asset_1.svg

Try:

Repo:my_repo "my image asset 1 svg"

这可能不是所有情况下的完美解决方案;我想它也可以匹配my-image-asset .svg这样的文件名。但是根据您的用例,它可能“足够好”?

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