How to create GitHub code permalinks
Pin a 40-char SHA blob permalink with #Lstart-Lend, post it via gh --body-file, then prove the stored URL with gh issue view --json.
What this covers
Creating a permanent link to a code snippet is a browser UI flow: select lines, copy the permalink, paste into a comment. Whole-file permanlinks are separate: Getting permanent links to files.
flowchart TD
Navigate["Navigate to file in repository"] --> Select["Click line number or range"]
Select --> Key["Press 'y' hotkey or click '...' menu"]
Key --> Permalink["URL changes to permanent commit SHA link"]
Permalink --> Embed["Paste into Issue/PR to render code snippet embed"]Link to code (UI)
- On GitHub, open the repository.
- Find the code:
- From a file, navigate to that file.
- From a pull request, open Files changed, find the file, click View.
- Select lines:
- Single line: click the line number to highlight it.
- Range: click the first line number, then hold Shift and click the last line number.
- To the left of the selection, open the overflow /
…menu and click Copy permalink. - Open the conversation (issue, PR review comment, or discussion) where you want the snippet.
- Paste the permalink into the comment and click Comment.
Where the snippet renders
| Destination | What readers see |
|---|---|
| Comment in the same repository | Expanded code snippet for that commit |
| Comment in a different repository | Plain URL |
| Markdown file (README, docs) | Plain URL (snippets do not expand here) |
Link to lines in a Markdown file
Rendered Markdown hides line numbers. Load the file without Markdown rendering by appending ?plain=1 to the blob URL, then add #L plus the line (or range) the same way you would for code.
Example pattern:
https://github.com/OWNER/REPO/blob//README.md?plain=1#L14
That highlights line 14 in the plain README.md view.
Why permalinks matter
A normal blob URL on a branch tip moves when the file changes. A permalink includes a commit SHA, so review comments and issue citations keep pointing at the exact lines you meant. Prefer permalinks when you are citing code in a review or bug report.