如何链接到github上的特定行号 [英] How to link to specific line number on github

查看:255
本文介绍了如何链接到github上的特定行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以链接到github上的一个文件上的特定行号(我确信我以前见过)...

有人可以告诉我如何做到这一点吗? 解决方案

不要只链接到行号!请务必使用。





update 3/25/2016:在上面的例子中,我提到了 README文件。当这个答案被写入时,那些非规范的网址实际上工作。但是现在这些URL已经不再工作,因为 README 被移动到 README.md 。但是,带有SHA哈希的规范URL仍然可以正常工作。


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)...

Can someone tell me how to do this?

解决方案

Don't just link to the line numbers! Be sure to use the canonical URL too. Otherwise when that file is updated, you'll have a URL that points to the wrong lines!

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. You literally click on the 18 at the left side, not the line of code. Looks like this:

And now your browser's URL looks like this:

https://github.com/git/git/blob/master/README#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:

And now your browser's URL looks like this:

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

Here's the important part:

Now get the canonical url for that particular commit by pressing the y key. The URL in your browser will change to become something like this:

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. That means that this link will work forever and not point to lines 18-20 of whatever future version of that file might contain.

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. Please upvote @watashiSHUN's answer too.

update 3/25/2016: Case in point -- in the example above, I referred to the "README" file in the url. 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. But the canonical URL with SHA hash still works, just as expected.

这篇关于如何链接到github上的特定行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆