How to refer to a specific line or range of lines in github? - github

If you want to add a reference to a specific line or a range of lines, it would be nice to get an URL that does that.

Note that the problem with the #Lxx-Lyy is that (from this post):
source files are subject to change, or even be removed, so there’s no guarantee that your link will always point to the correct place.
By default, GitHub project pages link to the most current version of the source. A better practice is to link to a specific commit, where the content of source files are not subject to versioning.
To view a GitHub project at a certain commit, click on the tree link in the commit header, or just press t on your keyboard. You can then browse the project files, and link to sources of this commit, i.e.
http://github.com/jquery/jquery/blob/27291ff06ddb655f90a8d1eada71f7ac61499b12/src/css.js#L171-L185.
Note that the only difference in the URL is changing the branch name master with the commit SHA.
+1 tip from Paul Irish:
plus you only need 4 characters of the SHA in the URL... it figures it out.
I usually truncate to 7ish characters.
Nice: http://github.com/jquery/jquery/blob/27291ff/src/css.js#L171-185
If you have a GitHub page which does not reference a sha1, type y:
that will reload that same page with the current SHA1.

If you go the the file (or version of the file you want to refer to), click the line number (or hold shift down to select a range).
=> you will see that the URL changes (it adds something like #L21-L23 to the URL).
Just copy the URL.

Related

How to get permanent link to a file with BitBucket server?

Sometimes I want to provide a link to some source code in the comment to an issue.
If I open repository in browser and find the fileI need, the link refers to master branch by default. It means that over time the code referred by the link will change, because the master will move together with commits history.
In GitHub and GitLab when you want to get a permanent link on a file you just opened, it is enough to press y on the keyboard and the link will switch from referring the master branch to particular commit hash it points to.
I.e. https://github.com/octocat/Hello-World/blob/master/README -> https://github.com/octocat/Hello-World/blob/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d/README
At work I use BitBucket server (v7.10.1, if it matters) and unfortunately the y does not work, and I also do not see any button on UI to get permanent link. How can I achieve the same result (get permanent link) on BitBucket server?
I know a couple of tricks I am not satisfied with:
using Bitbucket Linky plugin for Intellij, but I want a more light-weight way
pressing History button on a file, and choosing the most recent one (but it does not give you the master hash, it gives the hash of the last change of the file. And also this button won't work if the file has a single version and never changed after the 1st commit)
Got to the version you want, and then click 'Raw file' (top right hand side of screen)
The path in the address bar should now be a permanent link. It will be the raw version of the file unfortunately, but it is a permanent link.

Link to a specific branch, tag or commit, or a specific file within it, or a specific line within that file?

I create a tutorial, where I want to introduce some testing frameworks like Mockito, WireMock and EasyMock. I want to explain it step by step. Additionaly I want to provide examples, where the reader of the tutorial can try to recreate these. Therefore I want to to give them the opportunity to look into my source code and use it when they are not able to recreate the test. And then they can use the actual version to continue and then again, when they are not able to create the next test, I want to to give them the possibility to use my new version with the new test.
My idea is to use different commits and link them to the tutorial. How can I do this or is there any better approach to reach what I just explained?
Easy Peasy!
The basic approach is this: browse to exactly what you want on GitHub, and then share the URL. GitHub URLs are stable permalinks, even when you navigate to an old revision or another branch.
Open the repo in Github.
Browse to Branch, tag or commit you want.
For branch/tag, there's a drop down in the upper left of the files listing.
For a specific commit:
Click on Commits at the top of the files listing. This will take you to a page showing all the diffs. (You can share this URL if you want. Otherwise continue to the next step.)
Clink on Browse files in the upper right corner.
You are now looking at a repository view just like the one where you started, except this view is of a specific branch, tag or commit. It's like a GitHub time machine.
If you want to link to the entire repo at this branch/tag/commit, copy the URL right now.
If you want to link to a particular file at this branch/tag/commit, navigate to the file in the listing, then copy that URL.
If you want to link to a particular line in the file, click on a line number, then click on the ... button that appears next to it and then click Copy permalink
If you want to link to a range of lines in the file, hold down the shift key and clink on the other end of the range of lines. The lines will be highlighted. Click on the ... button that appears next to it and then click Copy permalink
All that said, unless you are walking them through your many iterations of the code, I'm not sure giving them links to versions in git history is the way to go. Let's say they want to clone your repo. Then they'd have to git checkout example-2-version-3 or something.
The more typical approach is to put the different versions in a directory hierarchy, making it far easier to browse, whether on GitHub or a local clone of your repo.

Linking to a line of code in a particular version of a file in Azure DevOps (VSTS)

I want to link to a line of code in such a way that the link will continue to work even when the file is updated in future commits.
In Github I would do this by pressing 'y' to move to a version of the page that includes the blob SHA:
https://github.com/rails/rails/blob/b49e38b76b0998b0a8312d8c08c98728d3de2006/activerecord/lib/arel/attributes/attribute.rb#L30
(Alternatively GitHub has a 'copy permalink' option in the '...' menu that appears in the margin when you select a line - documented here.)
Is there an equivalent in Azure DevOps?
The link that I get when I select a line has the form:
https://.../_git/project?path=XXXX&version=GBmaster&line=426&lineStyle=plain&lineEnd=427&lineStartColumn=1&lineEndColumn=1
Lots of parameters defining the selection but nothing pinning the file version.
Edit:
The below illustrations can be summarized in a few keyboard shortcuts.
Open the repo Files view using the 'e' global shortcut
Use 't' to put focus on the path selection to quickly navigate to the file in question
point 2 and 3 are order agnostic in relation to one another
Use 'y' to change the Files view to be based on the latest commit instead of on the branch
Make selection
Copy link as shown in illustration below
Browse files on the commit
Make sure you're browsing the entire source for the commit. Selecting lines when viewing the commit itself (ie AzDOs fancy git show) is for adding comments to the commit, but it doesn't help produce a link to the line # that version, nor is there a way (at least not that I could find) to get a link to the comment made on the line. Getting a link to a comment on a line in a commit might have been helpful to provide additional direct context to what is linked and why, but that's also not what you asked for.
don't click on the commit itself
make sure you're browsing all files as of a commit (not a branch)
Make selection
You should be able to select a line in the file contents and get a link to that line(s) in that version.
note: the link produced here is even more lengthy than the one you provided in your question.
Joy
When you navigate to the copied link, you will be directed to the commit and file contents with the line highlighted.
Understand, this link is bound to the commit you used to create it not to the tip of the branch. I expect this was how links are surfaced to be more sure that the link wouldn't break or lose context as the commit at the tip of the branch changes over time.
Thank you #JoshGust. For anyone else who wants the abbreviated version:
Files -->
(your repo branch) -->
History -->
Click A Commit hash/number (Latest/Head usually) -->
Browse Files -->
Your file -->
Select text & get a link.
Sheesh... quite some hoops to jump through, but glad it's possible.
To create a web (repository) link to a specific line from within your IDE, there are extensions with options to link to files, selections, to a branch or to a specific commit.
I am using these ones:
Editor
Plugin
Visual Studio Code
Git Web Links for VS Code
Visual Studio 2022
Git Web Links

Merge MS Word documents in github

I have a github repository and with a collaborator I am working on two version of the same Word document (.docx file).
I created a branch, but when I ask to make the merge between the two files, I cannot find the differences between the two files, as in the
picture.
Clicking on view, the message I receive in the middle of the page is:
Sorry about that, but we can’t show files that are this big right now.
When I tried to make the merge, the file of the branch version was simply overwritten on the master one (something that I do not want).
How can I merge them, checking all the new edits?
If you mean merging parts of text inside the files, you can't do that on Github. It doesn't understand .docx files (that's why it calls them "binary files" which is a generic designation).
You can either go with text files that Gitub can handle, or do the merges locally (after downloading from Github) using Microsoft Word itself.

Do GitHub repos show the current repo tag somewhere?

Right now to find out the current tag of a repo I have to clone the repo and run git tag.
Is the tag published somewhere on github?
Just want to make sure I'm not overlooking it somewhere.
The following formatted url will display the list of tags of the repository, with the most recent at the top
https://github.com/{:owner}/{:repository}/tags
You also can access this list by clicking on the Releases link on the main page of the repository, then on the Tags link.
Note: FWIW, in git parlance, there's no such thing as a current tag.
Tags are basically labels, names. They can exist in two flavors:
lightweight pointers to some commits (or trees (think directories) or even blobs (think files))
annotated pointers to the same kind of git objects as above. The annotation adds some additional information as the identity of the person who created the tag, the date of the tagging and optionally a cryptographic signature.
Tags are most of the time used to stamp a version. They can also be used to "bookmark" a specific object (while doing a code review, for instance).
So depending of what one use tags for, the current tags may be:
The tag pointing to the most recent commit
The latest created tag (if annotated)
The last one when their names are alpha sorted (or whatever sort mechanism matching the tag naming convention for this specific repository)