如何在 GitHub 上搜索以获取精确的字符串匹配,包括特殊字符

问题描述:

我可以使用像 "system <<-" 这样的引号从 Google 搜索完全匹配.

I can search exact matches from Google by using quotes like "system <<-".

如何为 GitHub 做同样的事情?

How can I do the same thing for GitHub?

你不能.GitHub官方搜索规则:

You can't. The official GitHub searching rules:

由于搜索代码的复杂性,有一些限制关于如何执行搜索:

Due to the complexity of searching code, there are a few restrictions on how searches are performed:

  • 仅考虑默认分支.在大多数情况下,这将是 master 分支.
  • 只能搜索小于 384 KB 的文件.
  • 只能搜索少于 500,000 个文件的存储库.
  • 在搜索源代码时,您必须始终包含至少一个搜索词.例如,搜索 language:go 是无效的,而 amazing language:go 是.
  • 搜索结果最多可以显示来自同一文件的两个片段,但文件中可能会有更多的结果.
  • 您不能在搜索查询中使用以下通配符:
    ., : ;/` ' "= *!?# $ &+ ^ |~ <>( ) { } [ ]
    搜索将简单地忽略这些符号.
  • 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.


克隆并使用git-grep:

git 支持使用 git-grep 在源中搜索命令.只需克隆一个存储库并使用文件夹中的命令:


Clone and use git-grep:

git support searching in sources with git-grep command. Just clone a repository and use the command in the folder:

git grep "text-to-search"

替代方案:

我建议您尝试 ripgrep 工具,它既快速又简单.像 git-grep 一样工作,但看起来更好:

I recommend you to try ripgrep tool, it's fast and simple. Works like git-grep but looks nicer:

rg "text-to-search"

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

And you can use the standard grep to search any text in files:

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