• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (deprecated)
You are here: Home / Dev Tips / GitHub Link to Commit instead of Branch

GitHub Link to Commit instead of Branch

Last updated on August 15, 2023 by Sal Ferrarello

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

https://github.com/salcode/stop-emails/blob/b225bd1ba6dcbeff80d443064d7f10d5864f9d40/stop-emails.php#L116-L119

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.

https://github.com/salcode/stop-emails/blob/b225bd1ba6dcbeff80d443064d7f10d5864f9d40/stop-emails.php#L116-L119,

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 .

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP and JavaScript developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a principal engineer.

Filed Under: Computing, Dev Tips, Recommendations Tagged With: GitHub

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2026 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in