gh-url script to get GitHub url for a file

The Problem

For the past decade or so, I’ve noticed a trend amongst my coworkers. When they need to look at the contents of a file that they’re not currently editing, they will go to GitHub, and click their way down through the folder structure until they eventually find the file they want to see the contents of.

On a related note, I believe that most of my coworkers don’t know how to take a relative path in a repo, and tell their text editors to open it.

For example, let’s assume we’re working in the same repo, and you’ve already got a file open in it in your editor. If I were to tell you to open foo/bar/baz.rb would you know how to tell your editor to open that file directly, without manually clicking to open the foo and bar folders and then clicking on the baz.rb file? For most developers the answer seems to be “no”, even though their editors can probably do it easily.

So, if I need to communicate the file I want you to look at, how do I do so in a way that makes your life as easy as possible, assuming you’re like most devs?

Easy, I give you a link to the file in GitHub. But how do i easily acquire that link?

The Solution

gh-url is a script I wrote to take a given path within a repo and generate a GitHub url for it, and optionally, open it in your browser.

So, for example i could say take that foo/bar/baz.rb path and say gh-url foo/bar/baz.rb and it would output a link to that file in GitHub. It takes care of making sure it links to the right branch, and if you have multiple remotes, it asks you which one. Just run it without any arguments to see the usage instructions.