简体   繁体   English

如何链接到github上的特定行号

[英]How to link to specific line number on github

I know I can link to a specific line number on a file on a GitHub repo (I'm sure I've seen this before)...我知道我可以链接到 GitHub 存储库上文件上的特定行号(我确定我以前见过这个)...

Can someone tell me how to do this?有人可以告诉我该怎么做吗?

Don't just link to the line numbers!不要只链接到行号! Be sure to use the canonical URL too.一定要使用规范的 URL Otherwise when that file is updated, you'll have a URL that points to the wrong lines!否则,当该文件更新时,您将有一个指向错误行的 URL!

How to make a permanent link to the right lines:如何永久链接到正确的行:

Click on the line number you want (like line 18), and the URL in your browser will get a #L18 tacked onto the end.单击所需的行号(如第 18 行),浏览器中的 URL 将在末尾#L18 You literally click on the 18 at the left side, not the line of code.您实际上是单击左侧的18 ,而不是代码行。 Looks like this:看起来像这样:

选择第 18 行

And now your browser's URL looks like this:现在您的浏览器 URL 如下所示:

https://github.com/git/git/blob/master/README.md?plain=1#L18

If you want multiple lines selected, simply hold down the shift key and click a second line number, like line 20. Looks like this:如果要选择多行,只需按住 shift 键并单击第二个行号,例如第 20 行。看起来像这样:

在此处输入图像描述

And now your browser's URL looks like this:现在您的浏览器 URL 如下所示:

https://github.com/git/git/blob/master/README.md?plain=1#L18-L20

Here's the important part:这是重要的部分:

Now get the canonical url for that particular commit by pressing the y key.现在通过按y键获取该特定提交的规范 url。 The URL in your browser will change to become something like this:浏览器中的 URL 将变为如下所示:

https://github.com/git/git/blob/5bdb7a78adf2a2656a1915e6fa656aecb45c1fc3/README#L18-L20

That link contains the actual SHA hash for that particular commit, rather than the current version of the file on master .该链接包含该特定提交的实际 SHA 哈希,而不是master上文件的当前版本。 That means that this link will work forever and not point to lines 18-20 of whatever future version of that file might contain.这意味着该链接将永远有效,并且不会指向该文件可能包含的任何未来版本的第 18-20 行。

Now bask in the glow of your new permanent link.现在沐浴在新永久链接的光芒中。 ;-) ;-)

update 9/29/2017: As pointed out by @watashiSHUN, github has now made it easier to get the permanent link by providing a ... menu on the left after you select one or more lines. 2017 年 9 月 29 日更新:正如@watashiSHUN 所指出的,github 现在通过在您选择一行或多行后在左侧提供一个...菜单,更容易获得永久链接。 Please see @watashiSHUN's answer too.请参阅@watashiSHUN 的回答

GitHub 永久链接菜单

update 3/25/2016: Case in point — in the example above, I referred to the "README" file in the URL. 2016 年 3 月 25 日更新:举个例子——在上面的例子中,我提到了 URL 中的“README”文件。 Those non-canonical urls actually worked when this answer was written.编写此答案时,这些非规范网址实际上有效。 But now those urls no longer work since README was moved to README.md .但是现在这些 url 不再有效,因为README被移动到README.md But the canonical URL with SHA hash still works, just as expected.但正如预期的那样,带有 SHA 哈希的规范 URL 仍然有效。

update 4/17/2022: Some files are "renderable" like markdown files. 2022 年 4 月 17 日更新:某些文件是“可渲染的”,如降价文件。 Github requires ?plain=1 to show the content of them instead of rendering. Github 需要?plain=1来显示它们的内容而不是渲染。

@broc.seib has a sophisticated answer , I just want to point out that instead of pressing y to get the permanent link, github now has a very simple UI that helps you to achieve it @broc.seib 有一个复杂的答案我只想指出,不是按y来获取永久链接,github 现在有一个非常简单的 UI 可以帮助您实现它

  1. Select line by clicking on the line number or select multiple lines by downholding shift (same as how you select multiple folders in file explorer)通过单击行号选择行或通过按住shift选择多行(与在文件资源管理器中选择多个文件夹的方式相同) 在此处输入图像描述

  2. on the right hand corner of the first line you selected, expand ... and click copy permalink在您选择的第一行的右上角,展开...并单击copy permalink 在此处输入图像描述

  3. that's it, a link with selected lines and commit hash is copied to your clipboard : https://github.com/python/cpython/blob/c82b7f332aff606af6c9c163da75f1e86514125e/Doc/Makefile#L1-L4就是这样,带有选定行和提交哈希的链接被复制到您的剪贴板https://github.com/python/cpython/blob/c82b7f332aff606af6c9c163da75f1e86514125e/Doc/Makefile#L1-L4 ://github.com/python/cpython/blob/c82b7f332aff606af6c9c163da75f1e86514125e/Doc/Makefile#L1-L4

将代码片段的永久链接粘贴到拉取请求评论字段中

You can you use permalinks to include code snippets in issues, PRs, etc.您可以使用永久链接在问题、PR 等中包含代码片段。

References:参考:

https://help.github.com/en/articles/creating-a-permanent-link-to-a-code-snippet https://help.github.com/en/articles/creating-a-permanent-link-to-a-code-snippet

Click the line number, and then copy and paste the link from the address bar.单击行号,然后从地址栏中复制并粘贴链接。 To select a range, click the number, and then shift click the later number.要选择一个范围,请单击该数字,然后按 shift 单击后面的数字。

Alternatively, the links are a relatively simple format, just append #L<number> to the end for that specific line number, using the link to the file.或者,链接是一种相对简单的格式,只需将#L<number>附加到该特定行号的末尾,使用指向文件的链接。 Here's a link to the third line of the git repository's README :这是git存储库README第三行的链接:

https://github.com/git/git/blob/master/README#L3 https://github.com/git/git/blob/master/README#L3

带有突出显示的行和修改后的地址行的屏幕截图

Many editors (but also see the Commands section below) support linking to a file's line number or range on GitHub or BitBucket (or others).许多编辑器(但也请参阅下面的命令部分)支持链接到 GitHub 或 BitBucket(或其他)上文件的行号或范围。 Here's a short list:这是一个简短的列表:

Atom原子

Open on GitHub在 GitHub 上打开

Emacs Emacs

git-link混帐链接

Sublime Text崇高的文本

GitLink GitLink

Vim维姆

gitlink-vim gitlink-vim


Commands命令

  • git-link - Git subcommand for getting a repo-browser link to a git object git-link - Git 子命令,用于获取到 git 对象的 repo-browser 链接
  • ghwd - Open the github URL that matches your shell's current branch and working directory ghwd - 打开与你的 shell 的当前分支和工作目录匹配的 github URL

Related to how to link to the README.md of a GitHub repository to a specific line number of code与如何将 GitHub 存储库的README.md链接到特定行号的代码相关

You have three cases:你有三种情况:

  1. We can link to (custom commit )我们可以链接到(自定义提交

    But Link will ALWAYS link to old file version, which will NOT contains new updates in the master branch for example.但是 Link 将始终链接到旧文件版本,例如,它不会包含 master 分支中的新更新。 Example:例子:

     https://github.com/username/projectname/blob/b8d94367354011a0470f1b73c8f135f095e28dd4/file.txt#L10
  2. We can link to (custom branch ) like (master-branch).我们可以像(主分支)一样链接到(自定义分支)。 But the link will ALWAYS link to the latest file version which will contain new updates.但该链接将始终链接到包含新更新的最新文件版本。 Due to new updates, the link may point to an invalid business line number.由于新的更新,该链接可能指向无效的业务线编号。 Example:例子:

     https://github.com/username/projectname/blob/master/file.txt#L10
  3. GitHub can NOT make AUTO-link to any file either to (custom commit) nor (master-branch) Because of following business issues:由于以下业务问题,GitHub 无法自动链接到(自定义提交)或(主分支)的任何文件:

    • line business meaning, to link to it in the new file业务含义,在新文件中链接到它
    • length of target highlighted code which can be changed可以更改的目标突出显示代码的长度

Linking to line number on GitHub wiki链接到 GitHub wiki 上的行号

I was having some trouble finding how to link to a specific location on GitHub wiki and ended up finding this out.我在寻找如何链接到 GitHub wiki 上的特定位置时遇到了一些麻烦,最终发现了这一点。 This might be common knowledge but I didn't know this and someone else might find it useful.这可能是常识,但我不知道,其他人可能会觉得它有用。

When browsing a wiki file you can put your mouse over a specific header text and a link icon will apear.浏览 wiki 文件时,您可以将鼠标放在特定的标题文本上,然后会出现一个链接图标。 Clicking this link icon, you're browser navigation url will be updated (ex: https://github.com/user/repository/wiki/Test#title-1 ).单击此链接图标,您的浏览器导航 url 将被更新(例如: https://github.com/user/repository/wiki/Test#title-1 ://github.com/user/repository/wiki/Test#title-1)。 This would be the link to that specific location on the wiki file.这将是指向 wiki 文件中特定位置的链接。

在此处输入图像描述

If you want to get a preview rendering of a markdown code snippet , after copying the link as mentioned in the other answer , remove the ?plain=1 parameter from the link.如果您想获得markdown 代码片段的预览呈现,请在复制另一个答案中提到的链接后,从链接中删除?plain=1参数。

For example, the following will show a plain text link:例如,以下将显示纯文本链接:

github.com/<organization>/<repository>/blob/<branch_name>/README.md?plain=1#L14

After removing the ?plain=1 parameter, it will show the preview of the code:去掉?plain=1参数后,会显示代码预览:

github.com/<organization>/<repository>/blob/<branch_name>/README.md#L14

带有预览的降价链接

See GitHub Docs: Creating a permanent link to a code snippet for more information.有关更多信息,请参阅GitHub 文档:创建指向代码片段的永久链接

For a line in a pull request.对于拉取请求中的一行。

https://github.com/foo/bar/pull/90/files#diff-ce6bf647d5a531e54ef0502c7fe799deR27
https://github.com/foo/bar/pull/
90 <- PR number
/files#diff-
ce6bf647d5a531e54ef0502c7fe799de <- MD5 has of file name from repo root
R <- Which side of the diff to reference (merge-base or head). Can be L or R.
27 <- Line number

This will take you to a line as long as L and R are correct.只要 L 和 R 正确,这将带您到一条线。 I am not sure if there is a way to visit L OR RIe If the PR adds a line you must use R .我不确定是否有办法访问 L OR RIe 如果 PR 添加一行,您必须使用R If it removes a line you must use L .如果它删除了一行,则必须使用L

From: Generate line pointer URL into Github Pull Request From: 将行指针 URL 生成到 Github Pull Request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM