Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer to a specific line or range of lines in github?

Tags:

github

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.

like image 622
Michael_Scharf Avatar asked Aug 06 '13 02:08

Michael_Scharf


People also ask

How do I link multiple lines in GitHub?

To link to multiple lines of highlighted code, select your first line of code and then CTRL+SHIFT click on the last line of code you want to highlight. Notice the URL is now appended with a range of line numbers (e.g. https://github.com/…/functions.php#L117-L148).

How do I link to a specific line in GitHub?

This is very simple to do. On any GitHub page, click on a line number to the left of the code. Notice the URL is now appended with the line number you selected (e.g. https://github.com/…/functions.php#L117). Visiting this link will take you to the exact line of highlighted code.

How do I create a code snippet in GitHub?

To add a code snippet: select the lines you want to reference, open the inline toolbar, click Copy permalink, and paste it anywhere.

How do I select multiple lines in bitbucket?

With just shift+click, a developer can select a range of lines, then add separate lines to the selection with command+click (or control+click), then copy the URL and share their selection with other developers.


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.

like image 73
VonC Avatar answered Oct 21 '22 17:10

VonC


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.

like image 36
Michael_Scharf Avatar answered Oct 21 '22 17:10

Michael_Scharf