# 搜索代码

。您可以通过在任何组合中使用这些代码搜索限定符来搜索代码并缩小结果范围。

可以跨所有 GitHub 执行全局搜索，或将搜索范围限于特定存储库或组织。 有关详细信息，请参阅 [关于在GitHub上的搜索](/zh/enterprise-server@3.22/search-github/getting-started-with-searching-on-github/about-searching-on-github)。

您只能使用这些代码搜索限定符搜索代码。 搜索代码时，专用于仓库、用户或提交的搜索限定符将不起作用。

> \[!TIP]
>
> * 本文包含 GitHub.com 网站上示例搜索的链接，但你可以在任何 GitHub 平台中使用相同的搜索筛选器。 在链接的示例搜索中，将 `github.com` 替换为 GitHub 平台的主机名。
> * 有关可以添加到任何搜索限定符以进一步改善结果的搜索语法列表，请参阅 [了解搜索语法](/zh/enterprise-server@3.22/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax)。
> * 对多个字词的搜索词使用引号。 例如，如果要搜索具有标签“In progress”的问题，可搜索 `label:"in progress"`。 搜索不区分大小写。

## 代码搜索的注意事项

由于搜索代码的复杂性，执行搜索的方式有一些限制：

* 必须登录到 GitHub 上的个人帐户才能跨所有公共存储库搜索代码。
* 仅当分叉的星多于父级存储库，且分支存储库在创建后至少有一个推送提交时，[分叉](/zh/enterprise-server@3.22/pull-requests/collaborating-with-pull-requests/working-with-forks/about-forks)中的代码才可搜索。 如果分叉的星少于父级存储库，或者无提交，则分叉**没有**针对代码搜索编制索引。 要在搜索结果中包含星数多于其父级的分叉，或者有至少一个推送提交，必须将 `fork:true` 或 `fork:only` 添加到查询中。 有关详细信息，请参阅“[在复刻中搜索](/zh/enterprise-server@3.22/search-github/searching-on-github/searching-in-forks)”。
* 只有 *默认分支* 被编制索引以用于代码搜索。
* 仅可搜索小于仅可搜索 5 MB 的文件。
* 每个文件只有前 500 KB 可被搜索。
* 最多可以搜索 4,000 个专用 存储库和内部 存储库。 这 4,000 个存储库将是你有权访问的前 10,000 个私人 和内部 存储库中最近更新的。
* 只有少于 500,000 个文件的存储库可搜索。
* 存档的存储库不可搜索。
* 除 [`filename`](#search-by-filename) 搜索外，在搜索源代码时，必须始终至少包含一个搜索词。 例如，搜索 [`language:javascript`](https://github.com/search?utf8=%E2%9C%93\&q=language%3Ajavascript\&type=Code\&ref=searchresults) 是无效的，而搜索 [`amazing language:javascript`](https://github.com/search?utf8=%E2%9C%93\&q=amazing+language%3Ajavascript\&type=Code\&ref=searchresults) 是有效的。
* 搜索结果最多可显示同一文件的两个分段，但文件内可能有更多结果。
* 无法使用以下通配符作为搜索查询的一部分：<code>. , : ; / \ \` ' " = \* ! ? # $ & + ^ | \~ < > ( ) { } \[ ] @</code>。 搜索只会忽略这些符号。

## 按文件内容或文件路径搜索

通过使用 `in` 限定符，可以将搜索限制为源代码文件的内容和/或文件路径。 如果省略此限定符，则只搜索文件内容。

| 限定符                                                                                                                       | 示例 |
| ------------------------------------------------------------------------------------------------------------------------- | -- |
| `in:file`                                                                                                                 |    |
| \[                                                                                                                        |    |
| **octocat in:file**]\(<https://github.com/search?q=octocat+in%3Afile&type=Code>) 匹配“octocat”在文件内容中出现的代码。                  |    |
| `in:path`                                                                                                                 |    |
| \[                                                                                                                        |    |
| **octocat in:path**]\(<https://github.com/search?q=octocat+in%3Apath&type=Code>) 匹配“octocat”在文件路径中出现的代码。                  |    |
| `in:file,path`                                                                                                            |    |
| \[                                                                                                                        |    |
| **octocat in:file,path**]\(<https://github.com/search?q=octocat+in%3Afile%2Cpath&type=Code>) 匹配“octocat”在文件内容或文件路径中出现的代码。 |    |

## 在用户或组织的仓库内搜索

要在特定用户或组织拥有的所有仓库中搜索代码，可使用 `user` 或 `org` 限定符。 要在特定存储库中搜索代码，可使用 `repo` 限定符。

| 限定符                                                                                                                                                                     | 示例 |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -- |
| <code>user:<em>USERNAME</em></code>                                                                                                                                     |    |
| \[                                                                                                                                                                      |    |
| **user:defunkt extension:rb**]\(<https://github.com/search?q=user%3Agithub+extension%3Arb&type=Code>) 匹配 @defunkt 中以 <em>.rb</em> 结尾的代码。                                |    |
| <code>org:<em>ORGNAME</em></code>                                                                                                                                       |    |
| \[                                                                                                                                                                      |    |
| **org:github extension:js**]\(<https://github.com/search?utf8=%E2%9C%93&q=org%3Agithub+extension%3Ajs&type=Code>) 匹配 GitHub 上以 <em>.js</em> 结尾的代码。                      |    |
| <code>repo:<em>USERNAME/REPOSITORY</em></code>                                                                                                                          |    |
| \[                                                                                                                                                                      |    |
| **repo:mozilla/shumway extension:as**]\(<https://github.com/search?q=repo%3Amozilla%2Fshumway+extension%3Aas&type=Code>) 匹配 @mozilla 的 shumway 项目中以 <em>.as</em> 结尾的代码。 |    |

## 按文件位置搜索

可使用 `path` 限定符搜索存储库中特定位置显示的源代码。 使用 `path:/` 可搜索位于存储库根目录级别的文件。 或者，指定目录名称或目录路径，以搜索位于该目录或其任何子目录中的文件。

| 限定符                                                                                                                                                                                              | 示例 |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -- |
| <code>path:/</code>                                                                                                                                                                              |    |
| \[                                                                                                                                                                                               |    |
| **octocat filename:readme path:/**]\(<https://github.com/search?utf8=%E2%9C%93&q=octocat+filename%3Areadme+path%3A%2F&type=Code>) 匹配在存储库根级别的包含“octocat”一词的 README 文件。                            |    |
| <code>path:<em>DIRECTORY</em></code>                                                                                                                                                             |    |
| \[                                                                                                                                                                                               |    |
| **form path:cgi-bin language:perl**]\(<https://github.com/search?q=form+path%3Acgi-bin+language%3Aperl&type=Code>) 匹配 cgi-bin 目录或其任何子目录中包含“form”一词的 Perl 文件。                                     |    |
| <code>path:<em>PATH/TO/DIRECTORY</em></code>                                                                                                                                                     |    |
| \[                                                                                                                                                                                               |    |
| \*\*                                                                                                                                                                                             |    |
| `console path:app/public language:javascript`                                                                                                                                                    |    |
| \*\*                                                                                                                                                                                             |    |
| ]\(<https://github.com/search?q=console+path%3A%22app%2Fpublic%22+language%3Ajavascript&type=Code>) 匹配 app/public 目录或其任何子目录（即使它们位于 app/public/js/form-validators）中的具有“console”一词的 JavaScript 文件。 |    |

## 按语言搜索

您可以基于所编写的语言搜索代码。
`language` 限定符可以是语言名称或别名。 有关支持的语言及其名称和别名的完整列表，请参阅 [github-linguist/linguist 存储库](https://github.com/github-linguist/linguist/blob/main/lib/linguist/languages.yml)。

| 限定符                                                                                                                                                                 | 示例 |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -- |
| <code>language:<em>LANGUAGE</em></code>                                                                                                                             |    |
| \[                                                                                                                                                                  |    |
| **element language:xml size:100**]\(<https://github.com/search?q=element+language%3Axml+size%3A100&type=Code>) 匹配标记为 XML 且刚好有 100 个字节的包含“element”一词的代码。             |    |
| <code>language:<em>LANGUAGE</em></code>                                                                                                                             |    |
| \[                                                                                                                                                                  |    |
| **display language:scss**]\(<https://github.com/search?q=display+language%3Ascss&type=Code>) 匹配包含“display”一词并标记为 SCSS 的代码。                                          |    |
| <code>language:<em>LANGUAGE</em></code>                                                                                                                             |    |
| \[                                                                                                                                                                  |    |
| **org:mozilla language:markdown**]\(<https://github.com/search?utf8=%E2%9C%93&q=org%3Amozilla+language%3Amarkdown&type=Code>) 匹配标记为 Markdown 的来自所有 @mozilla 存储库的代码。 |    |

## 按文件大小搜索

可以使用 `size` 限定符基于代码所在文件的大小搜索源代码。
`size` 限定符使用[大于、小于和范围限定符](/zh/enterprise-server@3.22/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax)根据找到代码的文件的字节大小筛选结果。

| 限定符                                                                                                                                                               | 示例 |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | -- |
| <code>size:<em>n</em></code>                                                                                                                                      |    |
| \[                                                                                                                                                                |    |
| **函数大小：>10000 language：python**]\(<https://github.com/search?q=function+size%3A%3E10000+language%3Apython&type=Code)匹配在大于> 10 KB 的文件中查找包含“function”这个单词的Python代码。 |    |

## 按文件名搜索

`filename` 限定符匹配具有特定文件名的代码文件。 您还可以使用文件查找器在仓库中查找文件。 有关详细信息，请参阅“[在GitHub上查找文件](/zh/enterprise-server@3.22/search-github/searching-on-github/finding-files-on-github)”。

| 限定符                                                                                                                                                                                             | 示例 |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -- |
| <code>filename:<em>FILENAME</em></code>                                                                                                                                                         |    |
| \[                                                                                                                                                                                              |    |
| **filename:linguist**]\(<https://github.com/search?utf8=%E2%9C%93&q=filename%3Alinguist&type=Code>) 匹配名为“linguist”的文件。                                                                          |    |
| <code>filename:<em>FILENAME</em></code>                                                                                                                                                         |    |
| \[                                                                                                                                                                                              |    |
| **filename:.vimrc commands**]\(<https://github.com/search?q=filename%3A.vimrc+commands&type=Code>) 匹配具有“commands”一词的 .vimrc 文件。                                                                 |    |
| <code>filename:<em>FILENAME</em></code>                                                                                                                                                         |    |
| \[                                                                                                                                                                                              |    |
| **filename:test\_helper path:test language:ruby**]\(<https://github.com/search?q=minitest+filename%3Atest_helper+path%3Atest+language%3Aruby&type=Code>) 匹配 test 目录中名为 test\_helper 的 Ruby 文件 。 |    |

## 按文件扩展名搜索

`extension` 限定符匹配具有特定文件扩展名的代码文件。

| 限定符                                                                                                                                                                       | 示例 |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -- |
| <code>extension:<em>EXTENSION</em></code>                                                                                                                                 |    |
| \[                                                                                                                                                                        |    |
| **form path:cgi-bin extension:pm**]\(<https://github.com/search?q=form+path%3Acgi-bin+extension%3Apm&type=Code>) 匹配 <em>cgi-bin</em> 下包含单词“form”且扩展名为 <em>.pm</em> 的代码。   |    |
| <code>extension:<em>EXTENSION</em></code>                                                                                                                                 |    |
| \[                                                                                                                                                                        |    |
| **icon size:>200000 extension:css**]\(<https://github.com/search?utf8=%E2%9C%93&q=icon+size%3A%3E200000+extension%3Acss&type=Code>) 匹配大于 200 KB 的以 .css 结尾的包含“icon”一词的文件。 |    |

## 其他阅读材料

* [排序搜索结果](/zh/enterprise-server@3.22/search-github/getting-started-with-searching-on-github/sorting-search-results)
* [在复刻中搜索](/zh/enterprise-server@3.22/search-github/searching-on-github/searching-in-forks)