If you want to add a reference to a specific line or a range of lines, it would be nice to get an URL that does that.
2 Answers
Note that the problem with the #Lxx-Lyy
is that (from this post):
source files are subject to change, or even be removed, so there’s no guarantee that your link will always point to the correct place.
By default, GitHub project pages link to the most current version of the source. A better practice is to link to a specific commit, where the content of source files are not subject to versioning.To view a GitHub project at a certain commit, click on the tree link in the commit header, or just press t on your keyboard. You can then browse the project files, and link to sources of this commit, i.e.
http://github.com/jquery/jquery/blob/27291ff06ddb655f90a8d1eada71f7ac61499b12/src/css.js#L171-L185.
Note that the only difference in the URL is changing the branch name master with the commit SHA.
+1 tip from Paul Irish:
plus you only need 4 characters of the SHA in the URL... it figures it out.
I usually truncate to 7ish characters.Nice: http://github.com/jquery/jquery/blob/27291ff/src/css.js#L171-185
If you have a GitHub page which does not reference a sha1, type y:
that will reload that same page with the current SHA1.

- 1,262,500
- 529
- 4,410
- 5,250
-
I said '(or version of the file you want to refer to)' but your description is more precise :-) – Michael_Scharf Aug 06 '13 at 18:29
-
Is there a way to do same thing with pull requests? – llamerr Feb 21 '18 at 16:17
-
@llamerr not sure: what did you try? (you can describe that in a separate question, actually) – VonC Feb 21 '18 at 16:18
-
good idea, thanks https://stackoverflow.com/questions/48911087/how-to-select-comment-on-a-range-of-lines-in-github-pull-request – llamerr Feb 21 '18 at 16:41
-
FYI: the `#Lxx-xx` syntax and the SHA truncation features are both available on GitLab as well. – drmercer Jun 22 '18 at 19:17
-
@drmercer True. Although I don't remember if those features were there (on gitlab.com) 5+ years ago, when I originally wrote that answer. – VonC Jun 22 '18 at 19:48
-
How can I comment to the selected range of code ? (not in pull request but any ordinary repo) – Raihanur Rahman Jun 30 '22 at 05:44
-
@RaihanurRahman As far as I know, commenting on multi-line reference/range is only supported for PR (Pull Request) – VonC Jun 30 '22 at 06:45
If you go the the file (or version of the file you want to refer to), click the line number (or hold shift down to select a range).
=> you will see that the URL changes (it adds something like #L21-L23
to the URL).
Just copy the URL.

- 33,154
- 22
- 74
- 95