不要只链接到行号!一定要使用规范URL。否则,当该文件更新时,您将拥有一个指向错误行的URL!
如何创建正确行的永久链接:
单击您想要的行号(例如第18行),您浏览器中的URL末尾将添加#L18
。您实际上是点击左侧的18
,而不是代码行。看起来像这样:
现在你的浏览器URL看起来像这样:
https://github.com/git/git/blob/master/README.md?plain=1#L18
如果您想选择多行代码,只需按住Shift键并单击第二个行号,例如第20行。效果如下:
现在你的浏览器URL看起来像这样:
https://github.com/git/git/blob/master/README.md?plain=1#L18-L20
这里是重要的部分:
现在按下 Y 键获取该特定提交的规范 URL。浏览器中的 URL 将更改为类似以下内容:
https://github.com/git/git/blob/5bdb7a78adf2a2656a1915e6fa656aecb45c1fc3/README#L18-L20
该链接包含特定提交的实际SHA-1哈希,而不是master
上文件的当前版本。这意味着此链接将永久有效,而不会指向该文件未来版本中可能包含的第18-20行。
现在享受您的新永久链接的光辉吧。 ;-)
在本答案最初撰写后,watashiSHUN指出GitHub现在更容易获取永久链接。在选择一个或多个行之后,左侧提供了一个...
菜单,如下所示:
?plain=1
将会显示带有行号的源文件,例如https://github.com/git/git/blob/master/README.md?plain=1
。broc.seib 的答案很高级,但我只想指出,GitHub 现在有一个非常简单的 UI 来帮助你获取永久链接,而不是按 Y 键获得。
通过单击行号选择一行或通过按住 Shift 键选择多个行(与在文件资源管理器中选择多个文件夹的方法相同):
在您选择的第一行的右上角,展开...
并点击复制永久链接
就这样。选定的行和提交哈希的链接已复制到剪贴板中:
https://github.com/python/cpython/blob/c82b7f332aff606af6c9c163da75f1e86514125e/Doc/Makefile#L1-L4
点击行号,然后复制并粘贴地址栏中的链接。要选择范围,请单击数字,然后按住 Shift 键单击后面的数字。
或者,链接的格式相对简单,只需将#L<number>
附加到文件的链接末尾以获取特定行号。这是指向 git 存储库的 README 的第三行的链接:
与如何将GitHub存储库的README.md文件链接到特定代码行有关
您有三种情况:
We can link to (custom commit)
But Link will always link to an old file version, which will not contain new updates in the master branch for example. Example:
https://github.com/username/projectname/blob/b8d94367354011a0470f1b73c8f135f095e28dd4/file.txt#L10
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
GitHub can not make an auto-link to any file either to (custom commit) nor (master-branch). Because of following business issues:
?plain=1
参数。github.com/<organization>/<repository>/blob/<branch_name>/README.md?plain=1#L14
?plain=1
参数后,它将显示代码的预览:github.com/<organization>/<repository>/blob/<branch_name>/README.md#L14
查看GitHub文档:创建代码片段的永久链接获取更多信息。
对于拉取请求中的一行代码。
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
只要 L 和 R 正确,这将带您到一行。我不确定是否有访问 L 或 R 的方法。即,如果 PR 添加了一行,则必须使用 R
。如果删除一行,则必须使用 L
。
y
键...https://help.github.com/articles/getting-permanent-links-to-files/ - broc.seibL18-L20,L29
,并且人们可以通过CTRL
点击添加非连续行。我认为你的想法是一个很好的建议,将会非常有用。 - broc.seibREADME
是README.md
呢? - zwcloud