Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a link to source code in GitHub Issue?

Tags:

github

I want to reference certain lines from a file in a GitHub repository in an issue. How can I do this without copying and pasting manually? Can I use a link to the source code?

To clarify, here is the file that I want to reference code from:

mysource.py

1: def func1():
2:    for i in range(10);
3:        print 'hello world'
4: 
5: def func2():
6:     for i in range(10);
7:        print 'hello world'

I want to quote lines 5 to 7 of the file in my issue like this:

5: def func2():
6:     for i in range(10);
7:        print 'hello world'
like image 456
Toby Seo Avatar asked Dec 01 '25 21:12

Toby Seo


2 Answers

You can now do this by selecting a range of lines and using the create issue menu option:

Example

screen shot of the menu available after selecting lines in a source file on GitHub

...resulting in:

screenshot of the resulting GitHub issue created, with a code listing inline in the issue via a link

Steps

  1. Go to the source file
  2. Select the range of lines (e.g., click the first line of interest, then shift + click the last line)
  3. From the ... menu on the left, select create an issue
  4. An issue page will appear with a link to the lines, which renders as view of the source code.
like image 55
Richard Dallaway Avatar answered Dec 06 '25 07:12

Richard Dallaway


The key is to get a permalink to the code, that will be automatically converted to the code snippet by GitHub Markdown (GFM). After completing the steps below, the whole thing should look like this after you create an issue or comment:

GitHub issue comment with the code snippet

Live example

Follow these steps:

  1. Go to the page with source code that needs to be referenced. For example: https://github.com/roots/trellis-cli/blob/master/cmd/venv_hook.go
  2. Click on the line number next to the respective code. The selected line should now be colored with yellow, and the three dots button should appear next to the line number, like that:

    Three dots button near the line number

    Tip: if you want to reference multiple lines of code, click on the first line number, then hold Shift and press the second line number.
  3. Click on the three dots button, select "Copy permalink":

    Three dots button in active state, hovered on the option "Copy permalink"
  4. Go to the issue where you want to reference the code. Paste the permalink there. The link should look like that:
    https://github.com/roots/trellis-cli/blob/a389115eb35e3c322e084183dc41497ffd67e7e0/cmd/venv_hook.go#L25-L27
    Notice that the link to the source code now has a commit SHA in the URL instead of the branch name master.
like image 33
Vasiliy Artamonov Avatar answered Dec 06 '25 09:12

Vasiliy Artamonov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!