Use Commit Hashes when Sharing GitHub URLs

Sharing GitHub commit hashes directly works for ephemeral uses, but there is a safer way for links that need to last.

By default, when you click through a repository on the GitHub website, you're browsing the default branch. This is often useful, as you may want the latest version of each file.

(Notice below the blob/main segments — here main is the default branch.)

Files Will Change

While this may be great when browsing, it's problematic when sharing the URL for a few reasons:

  • Files can be deleted or renamed, which can lead to a 404 (page not found) error.
  • The purpose of a file you've shared may change over the life of the project, and the context in which you originally shared it may not remain relevant.
  • GitHub also lets you share specific lines in a file. This is particularly dangerous when using branch URLs — if code changes, the line numbers will likely no longer be relevant.

Use Commit Hashes when Sharing

To overcome this issue, you can use commit hashes when sharing the URL. The easiest way to do this when looking at a file on the default branch is to replace the default branch in the URL with the latest commit for that particular file.

Fortunately, you can grab this while viewing the file, as the latest relevant commit for that file is listed on the page.

Browsing Files at Commit

Alternatively, you can use the Browse Files button (after clicking the commit hash link) to browse the entire project at that hash.

You can do this from any commit screen. And generally, whenever a commit is shown in the GitHub app, it is a link leading to the commit detail view (shown above).

Let's Connect

Keep Reading

3 Ways to Add an Image to GitHub README

Images often come in handy alongside documentation. Here are a few methods for adding them to your README and other markdown files.

Jan 30, 2021

Free Alternatives to GitHub for Private Git Hosting

GitHub is super awesome, until you have to start paying for it. Check out two feature-full and FREE alternatives.

Jan 08, 2015

Is GitHub the Best CMS?

It seems like a silly question, given that GitHub is not typically thought about as a CMS. But I analyze the question anyways, using a series of 11 factors.

Aug 30, 2022