Share a Line Range of Code With a GitHub Permalink
Why a branch link drifts
To share lines of code that stay put, copy a permalink: a GitHub URL that carries a commit ID instead of a branch name, plus the line numbers you selected. A plain link to main can show different code next week.
When you view a file on GitHub, you usually see the version at the current head of a branch. GitHub's docs warn that this version can change as new commits are made, so the file contents might not be the same when someone looks at it later. Say you saved "lines 10 to 20" in your notes last month. Those numbers may now point at something else.
A commit ID pins the version
The fix is to put a commit ID in the URL where the branch name was. Here is the pattern from GitHub's own example:
https://github.com/github/codeql/blob/main/README.md
https://github.com/github/codeql/blob/b212af08a6cffbb434f3c8a2795a579e092792fd/README.mdThe first link follows the branch. The second links to the exact version of the file in that commit, so the content will not change. The docs add that any identifier that resolves to a commit works in the URL, including branch names, commit SHAs, and tags. A SHA is the only one that never moves: a branch name moves with every commit, and a tag can be moved by whoever maintains the repository.
If you later need to know when a line changed or disappeared, a pinned link tells you which version you were looking at, and you can find when a line left your notes with git log -S to trace what happened after that commit.
Select a line range
Now the part the title promises: pointing at specific lines. GitHub's snippet guide says you can create a permanent link to a specific line or range of lines in a specific version of a file or pull request.
One line or several
- Open the file in the repository. For a pull request, open Files changed, browse to the file, and click View.
- To select a single line, click its line number. The line is highlighted.
- To select a range, click the number of the first line. Then hover over the last line of the range, press Shift, and click its line number.
The whole range is highlighted, and the address bar picks up a line fragment, which is the part of the URL after the #.
Copy permalink
With the lines highlighted, click the kebab icon to the left of the line or range (its label reads "Code line X options"), then click Copy permalink. That copies a link tied to a specific commit, not to a branch, so it is the one you want in a note, a ticket, or a message.
Check the address before you paste. A branch name between blob/ and the file path means the link will drift. A long hexadecimal string means it is pinned.
The keyboard route
You can do most of this without the mouse. According to GitHub's keyboard shortcuts page, pressing y in the source code browsing view expands a URL to its canonical form. The permalink page describes the same shortcut: press y while viewing a file and the URL updates to a permalink to the exact version you see. Press ? on any GitHub page to see all available shortcuts.
For lines, the same page documents these:
| Keys | What it does | | --- | --- | | Shift + J | Highlights the line the cursor is on in the code file | | Shift + Option + C (Mac), Shift + Alt + C (Windows, Linux) | Opens the line menu for the selected line | | l | Jumps to a line in your code |
A quick sequence is: click a line to place the cursor, press y to pin the version, then open the line menu and choose Copy permalink.
Paste it into a comment
Paste the permalink into a comment on an issue or pull request and click Comment. GitHub shows the lines as a code snippet right in the conversation.
That has limits. Per the docs, this type of permalink renders as a snippet only in the repository it originated in. In other repositories, it renders as a plain URL. It also does not work in Markdown files, only in comments. A snippet link in a README stays a plain link.
Those same comments are where people mention each other and cross-reference work, so it pairs well with linking to issues, pull requests, and people in GitHub Markdown.
Linking lines in a Markdown file
A README normally renders as formatted text, so there are no line numbers to click. The snippet guide's workaround is to load the file without Markdown rendering by adding ?plain=1, then append #L and the line number:
github.com/<organization>/<repository>/blob/<commit_SHA>/README.md?plain=1
github.com/<organization>/<repository>/blob/<commit_SHA>/README.md?plain=1#L14The second link highlights line 14. The docs describe the fragment as #L followed by the line number or numbers.
Keep permalinks in your notes
A pinned link ages well. When you keep code references in a notes app, store the full commit URL, not the branch one, and add a short line saying what the lines do. If you collect snippets as a team, keeping team code snippets in Git or in an app covers where each approach fits.
In Carets, a permalink is just text in a Markdown file, so it stays a clickable link right next to the snippet it describes.
Frequently Asked Questions
Does a GitHub permalink change when the file is edited?
No. A permalink uses a commit ID instead of a branch name, so it points to the file exactly as it was in that commit. GitHub's docs say the file content will not change.
Why does my permalink show as a plain URL instead of a code snippet?
A snippet permalink renders as a code snippet only in the repository it came from. In other repositories it shows as a URL. It also renders in comments, not in Markdown files.
How do I link to lines in a README that renders as formatted text?
Load the file without Markdown rendering by adding ?plain=1 to the URL, then append #L and the line number, such as README.md?plain=1#L14.
Can I use a tag instead of a commit ID?
GitHub says any identifier that resolves to a commit works in the URL, including branch names, commit SHAs, and tags. Only a commit SHA is fixed forever; a branch name moves.