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?
@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
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)
on the right hand corner of the first line you selected, expand ...
and click copy permalink
https://github.com/python/cpython/blob/c82b7f332aff606af6c9c163da75f1e86514125e/Doc/Makefile#L1-L4
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. I am not sure if there is a way to visit L OR R. I.e If the PR adds a line you must use R
. If it removes a line you must use L
.
From: Generate line pointer URL into Github Pull Request
Related to how to link to the README.md
of a GitHub repository to a specific line number of code
You have three cases:
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. 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 AUTO-link to any file either to (custom commit) nor (master-branch) Because of following business issues:
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.
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). Here's a short list:
Open on GitHub
git-link
GitLink
gitlink-vim
You can you use permalinks to include code snippets in issues, PRs, etc.
References:
https://help.github.com/en/articles/creating-a-permanent-link-to-a-code-snippet