When linking to specific lines in a file to GitHub, it is preferable to link to those lines in the file for a specific commit (rather than for a specific branch). This is because the contents of a branch can change but the contents of a commit remain the same forever.
Example
If I want to link to these four lines in my stop-emails WordPress plugin
add_action( 'plugins_loaded', array( $this, 'replace_phpmailer' ) );
add_action( 'fe_stop_emails_log', array( $this, 'log_to_php_error_log' ) );
add_action( 'admin_notices', array( $this, 'warning' ) );
add_action( 'init', array( $this, 'load_textdomain' ) );
by default when I navigate to the file and select these four lines, my link will look something like
https://github.com/salcode/stop-emails/blob/master/stop-emails.php#L116-L119
which is a link that points to lines 116
through 119
in stop-emails.php
on the master
branch.
When it Breaks
If at some point, someone adds ten lines to the beginning of the stop-emails.php
file and pushes the changes to the master
branch, my link will now be pointing to the wrong set of lines.
How to Avoid this Issue
However, if I’m on the above GitHub link and I hit the y
key on my keyboard, the URL will change to something like
Notice, now instead of master
we are pointing at b225bd1ba6dcbeff80d443064d7f10d5864f9d40
. This way even if the contents of stop-emails.php
on the master
branch changes, we’ll still be pointing to the correct lines (in this now older version of the file).
See GitHub Documentation on Getting permanent links to files.
Bonus Behavior in GitHub Issues
Branch Link in an Issue
If you add a link to lines on a branch to a GitHub issue, e.g.
https://github.com/salcode/stop-emails/blob/master/stop-emails.php#L116-L119,
the URL is displayed in the issue.
Commit Link in an Issue
If you add a link to lines on a commit to a GitHub issue, e.g.
the code for those lines is displayed in this issue.
Alternative Method to Get Permalink
An alternative way to get this permanent link (a.k.a. permalink) to these lines, to use the Copy permalink
menu item.
See GitHub Documentation on Creating a permanent link to a code snippet .
Leave a Reply