Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link to line in compare view on github

Tags:

github

Similar to How to link to specific line number on github I'd like to link to a line of code on github. The difference is that I want to link to a line in the diff viewer. See for example this link:

https://github.com/git/git/commit/5bdb7a78adf2a2656a1915e6fa656aecb45c1fc3/#diff-fea9abc098557219301972e6c6782b8fL9

In addition to the commit hash (5bdb7a78...) there is a second hex string in the url anchor (#diff-fea9abc0...) that seems to specify the file being changed.

I'd like to be able to generate these links without first visiting github and clicking on the line. How is the second hex string (#diff-fea9abc0...) generated?

like image 842
user12341234 Avatar asked May 17 '17 15:05

user12341234


People also ask

How do I link to a specific line in GitHub?

You can link to a specific line in the Markdown file the same way you can in code. Append #L with the line number or numbers at the end of the url. For example, github.com/<organization>/<repository>/blob/<branch_name>/README.md? plain=1#L14 will highlight line 14 in the plain README.md file.

How do I compare in GitHub?

TLDR: Just add /compare at the end of the URL. You can use the Github Compare UI, which will generate the URL for you. Replace ORG and REPO with your values. The UI only lists branches, but you can also type in any valid Tags (e.g. v1.

How do I compare the differences between two branches in GitHub?

On the Github, go to the Source view of your project. You will see a link named 'Branch List'. Once the page opens you can see a list of all the remote branches. Hit on the Compare button in front of any of the available branches to see the difference between two branches.


1 Answers

The anchor hash is reference to the filename being linked to. In the question above, the linked line is pointing to line 9 (on the left hand side) of the file contrib/hooks/multimail/README.Git. To generate the hash for that file, simply MD5 hash it:

% md5 -s contrib/hooks/multimail/README.Git
MD5 ("contrib/hooks/multimail/README.Git") = fea9abc098557219301972e6c6782b8f

Then append L or R depending whether you’d like to reference the left (original) or right (changed) side in the patch, and append the line number:

               MD5 (filename)          Line number
      /------------------------------\ v
#diff-fea9abc098557219301972e6c6782b8fL9
                                      ^
                                    L or R
like image 142
user12341234 Avatar answered Oct 04 '22 00:10

user12341234