When you browse a repository on GitHub and open any text file, you can easily highlight single lines simply by clicking a line number on the left. Taking TypeScript's scanner.ts file as an example, a single-line highlight can look like this:
You can also highlight multiple lines by pressing SHIFT and clicking on the last line number of the range you want to select:
Your line selection will be reflected in the hash portion of the file URL, allowing you to send around the URL to point somebody to your selected line range:
The problem with these URLs is that they're not persistent. File contents change over time, and thus the line numbers are mapped differently as well.
Take a look at this TypeScript issue, for example, in which the author linked to a file that had some lines highlighted. It was supposed to have a
for-loop within the line range [272, 279]. When I followed the posted link at the time of writing this post, what I saw was the following line range, obviously not the one the author intended to highlight:
Depending on when you read this post, you might see something else entirely. That's not very helpful, and it gets even worse once a file has changed drastically.
Luckily, there's a solution to work around this issue: GitHub listens for the y key any time you're looking at an opened file. When you press y, you will be redirected to a URL that represents a permalink to the Git blob associated with that file.
That URL you're being redirected to uniquely identifies that blob — and only that blob. It also doesn't change over time, which makes it suitable for sharing.
Here's the blob URL to the aforementioned
for-loop with correct highlighting:
The next time you're about to highlight some lines in a text file on GitHub and share a link to them, remember to press y first to get a permalink!