6

Linking to a line number in GitHub is easy: clicking on the line edits the URL to point to that line so we can share it.

However, if the file changes with a new commit, the line numbers may offset and this renders the existing links as inaccurate and confusing.

How to permanently link to a line of code in GitHub? Too often I came across old links for which the code changed.

Yet I have found that I can pick a commit's diff and link to that diff like this, here, on line 150 of base.py for example: https://github.com/deepmind/sonnet/commit/60be2bb4ccd74230285c1c822452a99558915c50#diff-62bce8ca7517a19cdfdee428fc985bf1R150

But I think linking to a specific commit is confusing (or a bit overkill) since the commit may not concern at all what I want to point to in the file.

Would anyone have a cleaner way to link to line numbers in GitHub? (E.g.: pointing to a specific file version rather than a commit would be cleaner IMO)

Guillaume Chevalier
  • 9,613
  • 8
  • 51
  • 79
  • I don't think that would be possible, since the line itself might change or even get deleted. So you will always be stuck to have to link to a certain point in time if you do not want the code to change. – litelite Jul 17 '17 at 20:20
  • I prefer something like https://github.com/deepmind/sonnet/blob/master@%7B2017-07-18%7D/sonnet/python/modules/base.py#L150. – user4003407 Jul 17 '17 at 22:22

2 Answers2

6

You can link to a commit’s tree instead of its diff, and the easiest way to get this link is GitHub’s permalink keyboard shortcut: press Y after clicking the line on a branch’s tree.

If you’d like to get there from a commit instead of a branch, use the “Browse files” button in the header.

To construct it manually, the path is:

/blob/<hash>/<path...>#L<line>

Done with your example link:

https://github.com/deepmind/sonnet/blob/60be2bb4ccd74230285c1c822452a99558915c50/sonnet/python/modules/base.py#L150

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Great! The "y" shortcut must not be a capital letter thought. The `kbd` tag capitalized it despite you wrote it in lower case. – Guillaume Chevalier Jul 17 '17 at 20:39
  • 1
    @GuillaumeChevalier: The key labels on your keyboard aren’t uppercase? If I meant Shift+Y I’d write that. – Ry- Jul 17 '17 at 20:41
  • I have suggested an edit to the answer. What I mean is that it worked for me to press `y` and not `Y`. However, see here the fancy way of displaying a keyboard shortcut capitalizes the letter (at least, it capitalizes it in my Chrome for Linux installation): y. – Guillaume Chevalier Jul 17 '17 at 21:39
  • 1
    @GuillaumeChevalier: Y just means the Y key. There’s no uppercase or lowercase Y key on the keyboard. – Ry- Jul 17 '17 at 21:41
1

This is pretty much impossible.

The closest you can get is to point to a specific reference, e.g. by clicking "Browse files" from a commit:

https://github.com/deepmind/sonnet/blob/60be2bb/sonnet/python/modules/base.py#L150

A neater solution might be to link to a specific tag, assuming it contains your commit:

https://github.com/deepmind/sonnet/blob/v1.6/sonnet/python/modules/base.py#L150

However, you do run the risk in certain circumstances of that commit being removed from the repository's history. In a well-maintained public repository, though, both commit references and tags should provide suitable links.

cmbuckley
  • 40,217
  • 9
  • 77
  • 91