Search code examples
githubgithub-api

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?


Solution

  • 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.

    1. Open the repo in Github.
    2. 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:
        1. 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.)
        2. Clink on Browse files in the upper right corner.
    3. 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.