Before Publishing a Tutorial: Pin GitHub Code Links to a Commit

Create a GitHub permalink to a specific commit, verify the file and line range, and keep tutorial links accurate when example code changes.

Published
Reading time
6 min read
Before Publishing a Tutorial: Pin GitHub Code Links to a Commit
On this page

A GitHub permalink to a specific commit lets readers see the code your tutorial describes, even if the default branch changes later. Before publishing, replace links to moving branches with links to the exact revision you reviewed, then check the file and line range.

Key Takeaways

  • A branch URL follows the branch as it changes; a commit-pinned URL identifies a particular snapshot.
  • A tag URL can identify a release, but a tag can be moved or deleted. A full commit hash makes the intended revision explicit.
  • Check the repository, commit, file, and selected lines before publishing. A pinned link does not follow later edits.
  • Pinning preserves the reference, not access: repository permissions and availability still matter.

Imagine a fictional repository, example-owner/tutorial-repo, containing src/greeting.js. These paths illustrate the three link types; they are not links to a real example repository.

Link targetExample path after github.com/What it identifies
Branchexample-owner/tutorial-repo/blob/main/src/greeting.jsThe file as it exists at the current tip of main. Later commits can change what readers see.
Tagexample-owner/tutorial-repo/blob/v1.0/src/greeting.jsThe file at the revision currently referenced by v1.0. Tags are useful release labels, but can be changed or deleted.
Commitexample-owner/tutorial-repo/blob/0123456789abcdef0123456789abcdef01234567/src/greeting.jsThe file in the specified commit. The hash here is illustrative.

A line fragment such as #L3-L8 identifies a displayed range within the file. With a branch link, those line numbers can later refer to different code; with a commit link, they refer to the file at that commit.

Advertisement

The following workflow uses GitHub's file view. The current GitHub documentation and interface could not be checked for this article, so menu labels or placement may differ. The examples were not executed.

  1. Open the intended repository and file on GitHub. First ensure the selected branch or tag contains the version your tutorial discusses.
  2. In the file view, press y to switch the browser URL to a permalink for the displayed revision. Alternatively, look for a permalink or copy-permalink action in the file or line menu.
  3. To link to a single line, select its line number. For a range, select the first line number, then hold Shift and select the last line number.
  4. Copy the resulting URL from the browser address bar or the available copy-link action. Check that its revision segment is a commit hash, not a branch or tag name, and that the fragment contains the intended line or range.

If keyboard shortcuts or line selection do not work as described, use the commit-specific URL pattern below and inspect the result in the browser. Do not assume a copied line link is commit-pinned just because it includes line numbers.

If you have the repository locally, use Git to identify the commit you intend to cite. From the repository's root, in a shell with Git installed:

git rev-parse --verify 'HEAD^{commit}'

Copy the full hash printed by Git. Substitute it, along with the actual owner, repository, and file path, into this pattern:

https://github.com/OWNER/REPOSITORY/blob/FULL_COMMIT_HASH/PATH/TO/FILE#L3-L8

Replace L3-L8 with the relevant line numbers, or omit the fragment to link to the whole file. For a single line, use a fragment such as #L3. These are placeholders: use the actual path as displayed by GitHub, especially if it contains characters that need URL encoding.

HEAD is the commit checked out locally, which is not necessarily the branch tip or release you intended to document. Also make sure the commit has been pushed to the GitHub repository: a local commit hash alone does not make the file accessible there.

Verify the repository, revision, file, and lines

Open the copied link and check each part before publishing:

  1. Repository: Confirm the owner and repository are the intended ones, not a fork or a similarly named project.
  2. Commit: Confirm that the URL contains the full commit hash you chose. If you selected it locally, compare it with the result of git rev-parse above. Make sure it is present on GitHub.
  3. File: Confirm the path and contents match the tutorial. A file with the same name elsewhere in the repository may not be the example you meant.
  4. Line range: Confirm the highlighted lines include the complete example and any context the explanation depends on. Check for an off-by-one selection and avoid highlighting unrelated code.
  5. Reader access: Open the link in a session with the same access your readers will have. A private repository requires appropriate authorization; a link that works for an author may not work for the audience.

You can also inspect a local commit before linking. Replace FULL_COMMIT_HASH with the hash you selected and adjust the path:

git show FULL_COMMIT_HASH:src/greeting.js | cat -n

This displays the committed file with numbered lines in shells that support cat -n. Compare the relevant lines with GitHub's file view; the browser check is still necessary to confirm the repository is accessible and the URL opens as intended.

Advertisement

A commit-pinned link continues to identify the old snapshot; it does not track a renamed file or later edits. That is useful when the article explains the old code, but it can become misleading if the article is revised to describe a newer version.

When updating a tutorial:

  1. Decide whether readers should see the original example or the revised one. Keep the old link if the surrounding explanation still describes that snapshot.
  2. If the example has changed or moved, locate it at the new revision, select the relevant lines, and create a new commit-pinned link. Update the prose and any commands or output expectations at the same time.
  3. Recheck access and the complete line range. If the old repository or commit is no longer accessible, restore an accessible copy where appropriate or replace the link and explain the change in the article.

A pinned URL is not an archive or a guarantee of permanent availability: a repository can become private or disappear, and a commit may no longer be available from that repository. Keep any appropriate backup separately.

Common Mistakes

  • Copying a branch URL with a line fragment: The highlighted line numbers do not make the revision immutable.
  • Using a release tag as if it were a commit hash: Tags are convenient labels, but their references can change.
  • Linking to an unpushed local commit: Readers cannot open it on GitHub until it is available in the repository.
  • Checking only the URL: Open the link and inspect the actual file and selection, including permissions.
  • Updating only the link: A new commit may change the code's behavior, dependencies, or surrounding explanation.

Best Practices

Pin the revision that you actually reviewed, use the full commit hash, and link to the smallest line range that still provides enough context. For a tutorial covering a whole file, link to the file instead of forcing a narrow selection. Recheck links whenever you revise the article or its examples.

Conclusion

Use a commit-pinned GitHub link to connect a tutorial to the exact code it describes. Verify the destination and reader access, then create a new permalink whenever the tutorial moves to a different revision.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.