508

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sfletche
  • 47,248
  • 30
  • 103
  • 119

9 Answers9

760

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:

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:

Enter image description here

And now your browser's URL looks like this:

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. 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-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. https://github.com/git/git/blob/master/README.md?plain=1.

broc.seib
  • 21,643
  • 8
  • 63
  • 62
  • Nice addition! Thanks for chiming in (even if it is a year later)! – sfletche Jul 08 '15 at 02:47
  • 1
    I only chimed in because the accepted answer will produce an incorrect link when the code changes. Yikes! – broc.seib Aug 01 '15 at 15:07
  • 3
    The y key does nothing for me. Has this been changed? Trying from Google Chrome Dev Channel. – k0pernikus Dec 05 '15 at 23:39
  • 4
    It still works... Docs still say to use `y` key too... https://help.github.com/articles/getting-permanent-links-to-files/ – broc.seib Dec 06 '15 at 16:23
  • 4
    that shift+click the second line for a block is a good one! I was manually editing the URL to add the second line :). follow up question though, what if I wanted lines 2, 4, and from 17-22 highlighted? Is that possible or can I only highlight one block at a time? – penchant Mar 24 '16 at 23:33
  • 5
    @tr3buchet I just pinged github support -- it's not possible at the moment. They said they will consider the feature, but no promises. I offered that the format be something like `L18-L20,L29`, and that a person could `CTRL`-click to add non-consecutive lines. I think your idea is a great suggestion and would be very useful. – broc.seib Mar 25 '16 at 14:39
  • @broc.seib thanks for checking it out! I hope they do it at some point because using multiple links to communicate each section in the same gist is a bit painful.. I was thinking about adding it to https://github.com/tr3buchet/gister but i think it's a bit out of scope – penchant Mar 28 '16 at 19:37
  • This does not work for me; they `y`key does nothing. The key is also not listed in the "Keyboad Shortcut" list which is called forth by pressing `?`. A workaround would be to go to the desired commit on the "Commits" screen and selecting "Browse files" of that particular commit, then link from there. – Svaberg Sep 14 '16 at 12:57
  • @Svaberg what you see on github when you press `?` is context sensitive. While viewing a file (not a directory), you'll discover that `?` indeed lists `y` as an option, and does actually work. ;-) – broc.seib Sep 14 '16 at 23:22
  • @broc.seib you are right! I see it now. Thanks for the help. – Svaberg Sep 15 '16 at 08:30
  • **Now get the canonical url for that particular commit by pressing the y key.** <---- This line compelled me to upvote this answer. – Prashant Pokhriyal Sep 13 '17 at 08:54
  • 1
    I can see that using such a link in an issue will showup the lines as a code snippet when used in a discussion (issue, PR), but it doesn't work in README.md not in wikis. (related: https://help.github.com/articles/creating-a-permanent-link-to-a-code-snippet/) – ffflabs Jan 04 '18 at 20:34
  • 2
    What if `README` is `README.md`? – zwcloud Apr 11 '18 at 05:21
  • Is it possible to link to specific lines of a pull request in the pull request message itself? – Qwerty Jan 27 '20 at 15:20
  • How would this be done in Gists with multiple files? i.e. How would one highlight lines 12-15 of _SomeFragment.kt_ in [AssistedInjectModule.kt](https://gist.github.com/AdamSHurwitz/964a37c60161d74b80cb8a1e520f2bef)? – AdamHurwitz Jul 20 '20 at 03:36
  • 1
    @AdamHurwitz look like it is the same approach -- SHIFT-click the line numbers, and the URL in your browser window will be updated, which you can copy/paste. Alas, there is no "Copy Permalink" UI menu. [Lines 12-15 in SomeFragment.kt](https://gist.github.com/AdamSHurwitz/964a37c60161d74b80cb8a1e520f2bef#file-somefragment-kt-L12-L15) – broc.seib Jul 20 '20 at 17:30
  • Is it possible to render a code snippet from a link using the GitHub API? – Daniel Luca CleanUnicorn Nov 24 '20 at 12:36
  • The `y` key also updates to the canonical link in GitLab. Awesome! – Bloodgain Dec 15 '21 at 21:41
  • Instead of adding a bunch of updates to the answer, why not just replace it with the current working solution? – JBaczuk Jun 24 '22 at 13:00
  • It's very important to note that this only works for links to files on the same repo you made the comment on. I'm not sure why it's not documented *ANYWHERE*. – what the May 26 '23 at 21:11
87

broc.seib has a sophisticated answer, but 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.

  1. Select a line by clicking on the line number or select multiple lines by downholding Shift (the same as how you select multiple folders in File Explorer):

    Enter image description here

  2. On the right hand corner of the first line you selected, expand ... and click copy permalink

    Enter image description here

  3. That's it. A link with the selected lines and a commit hash is copied to your clipboard:

    https://github.com/python/cpython/blob/c82b7f332aff606af6c9c163da75f1e86514125e/Doc/Makefile#L1-L4

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
watashiSHUN
  • 9,684
  • 4
  • 36
  • 44
33

See:

A permalink to a code snippet is pasted into a pull request comment field

You can you use permalinks to include code snippets in issues, PRs, etc.

References:

Creating a permanent link to a code snippet

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
23

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.

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. Here's a link to the third line of the git repository's README:

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

Screenshot with highlighted line and the modified address line

Leigh
  • 12,038
  • 4
  • 28
  • 36
  • 3
    Don't forget that if the file changes, this URL will still point to line 3, which could contain new code, and may not be what you wanted! I've offered another solution that yields a permanent link. Hint: just press `y`. ;-) – broc.seib Jul 08 '15 at 02:36
  • 4
    The link is broken – pmrotule May 03 '17 at 09:48
7

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:

Atom

Open on GitHub

Emacs

git-link

Sublime Text

GitLink

Vim

gitlink-vim


Commands

  • git-link - Git subcommand for getting a repository browser link to a Git object
  • ghwd - Open the GitHub URL that matches your shell's current branch and working directory
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

Related to how to link to the README.md file of a GitHub repository to a specific line number of code

You have three cases:

  1. 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
    
  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 an auto-link to any file either to (custom commit) nor (master-branch). Because of following business issues:

    • line business meaning, to link to it in the new file
    • length of target highlighted code which can be changed
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
  • It's good that someone pointed this out. Wish it was easy to find a compromise, like binding to line and forward tracking where it's currently in the new version. OR at least have link that points to wherever the certain substring is needed. – vintprox Dec 04 '21 at 17:37
1

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

vaughan
  • 6,982
  • 6
  • 47
  • 63
1

Linking to a line number on a GitHub wiki page

I was having some trouble finding how to link to a specific location on a GitHub wiki page and ended up finding this out. 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 appear. Clicking this link icon, your browser navigation URL will be updated (example: https://github.com/user/repository/wiki/Test#title-1). This would be the link to that specific location in the wiki file.

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hozeis
  • 1,542
  • 15
  • 36
1

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.

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:

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

A link to a Markdown with preview

See GitHub documentation: Creating a permanent link to a code snippet for more information.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahozad
  • 18,032
  • 13
  • 118
  • 133
  • Linking to rendered markdown file (preview) doesn't work for me. It shows the file but does not scroll to the selected line. Linking to raw file (plain=1) works. Any thoughts? – gruby karol Nov 13 '22 at 07:29
  • @grubykarol since markdown is rendered on github.com, is not possible to link to a line in rendered state, but if you, or the people you want to share the link with, use chrome there is an alternative: use the [copy link to highlight](https://blog.google/products/chrome/more-helpful-chrome-throughout-your-workday/) feature on the markdown file in rendered state over the selected text – manus Jul 04 '23 at 20:12