Skip to content

Instantly share code, notes, and snippets.

@freenerd
Last active August 29, 2015 14:23
Show Gist options
  • Save freenerd/0c5a6ac9b61f8588361d to your computer and use it in GitHub Desktop.
Save freenerd/0c5a6ac9b61f8588361d to your computer and use it in GitHub Desktop.
GitHub Bookmarklet for Link File to Commit

This is a bookmarklet that transforms a link to a file from going to a branch to the specific current HEAD commit. This allows to keep links consistent, even when files change. This is specifically useful when linking to specific lines (which loose their meaning over time when line numbers change).

This is the transformation:

Before

https://github.com/mapbox/mapbox.js/blob/mb-pages/package.json#L25

After

https://github.com/mapbox/mapbox.js/blob/e13cd8b00072fc208cd4670411292478e0718b91/package.json#L25

To install, create a new bookmark and copy the code below in their:

javascript:(function(){
    var a = $('.commit-title a');
    if (a && a.length > 0) {
        s = a[0].href.split('/');
        w = window.location.pathname.split('/');
        w[4] = s[s.length-1];
        window.location.pathname = w.join('/');
    } else {
        alert('Not a Github file site or bookmarklet is broken');
    }
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment