GitHub file linking to particular line - how to do it?

I remember seeing somewhere that you can create links that jump directly to a specific line in a GitHub file. This would be really helpful when I want to share code snippets with my team or reference exact locations in files during discussions.

I’ve tried a few different approaches but can’t seem to get it working properly. Does anyone know the correct syntax or method to create these direct line links? I’m working with both public and private repositories if that makes any difference.

Any help would be appreciated since this feature would save me a lot of time when doing code reviews and documentation.

Pro tip: hold shift while clicking line numbers to auto-select a range and update the URL. Saves you from typing it manually. Works on GitHub mobile too, just with a slightly different interface.

Pro tip: hit ‘Y’ when viewing any GitHub file to get a permalink with the commit hash. Your line references won’t break when someone updates the file later. Super handy for bug reports or specs where you need to point to exact code. The permalink keeps your line number too - so if you’re on line 35 and press Y, you’ll get the commit hash plus #L35 in the URL. Can’t tell you how many times this has saved me when digging up old discussions months later.

You can grab these line-specific URLs straight from GitHub without learning the syntax. Just right-click any line number when viewing a file and hit “Copy permalink.” GitHub spits out the full URL with the commit hash and line reference already built. Way faster than typing URLs manually, especially when you’re bouncing between files while debugging. The link looks like https://github.com/user/repo/blob/abc123/file.py#L67 and works great in Slack, emails, or docs. No chance of screwing up the URL format either.

You can also build the URL manually if you know the line number. Just add #L plus the line number after the file path. I like using commit hashes instead of branch names in the URL - that way the link always points to the exact code version you’re talking about. Super helpful during code reviews since main keeps changing. For multiple lines, use #L25-L30 to highlight a range. These links work in PR comments and issues too, which makes collaboration way easier.

To create links that direct to a specific line in a GitHub file, simply click on the line number in the file’s view. This action updates the URL to include #L followed by the line number. For instance, the URL https://github.com/user/repo/blob/main/file.js#L42 will take you to line 42 directly. If you need to link multiple consecutive lines, you can use the format #L10-L15 to reference lines 10 through 15. This method works with both public and private repositories, and the links remain functional even if branches or commits are modified.