How to link to a specific line number on GitHub


I know I can link to a specific line number on a file on a GitHub repository (I'm sure I've seen this before)...

How can I 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:

line 18 selected

And now your browser's URL looks like this:

If you want multiple lines selected, simply hold down the Shift key and click a second line number, like line 20. Looks like this:

Enter image description here

And now your browser's URL looks like this:

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:

That link contains the actual SHA-1 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. ;-)

After this answer was originally written, watashiSHUN pointed out that GitHub has now made it easier to get the permanent link. A ... menu is provided on the left after you select one or more lines, like this:

GitHub permalink menu

Please see watashiSHUN's answer too.

Note that GitHub does not show line numbers for some "renderable" files, like Markdown files. Adding ?plain=1 to the url will show the source file with line numbers, e.g.

Staging Deleted files

How to move some files from one git repo to another (not a clone), preserving history