Linking to Github

It was rightly pointed out yesterday that it’s dangerous to link to lines or blocks of code on Github without explicitly specifying the commit hash in the URL. On this theme, consider this git command:

$ git browse -u -- commit/$(git rev-parse HEAD)
https://github.com/tangentlabs/django-oscar/commit/17851d4b66922f8d7e203e2b469040690c84a0db

This emits the Github URL to the HEAD commit on the current branch, specifying the commit hash in the URL. Note that the browse subcommand is provided by the excellent hub library.

Pasting links to commits is common, both for mailing list posts and within discussion on Github itself. Getting the correct URL quickly is useful.

We can streamline further using an alias:

# ~/.gitconfig

[alias]
url = !hub browse -u -- commit/$(git rev-parse HEAD)

so we can run:

$ git url
https://github.com/tangentlabs/django-oscar/commit/17851d4b66922f8d7e203e2b469040690c84a0db

to get the expanded HEAD URL. Even better, we can parameterise:

# ~/.gitconfig

[alias]
url = "!f() { sha=$(git rev-parse ${1:-HEAD}); hub browse -u -- commit/$sha; }; f"

so we can now specify a commit:

$ git url
https://github.com/tangentlabs/django-oscar/commit/17851d4b66922f8d7e203e2b469040690c84a0db

$ git url head
https://github.com/tangentlabs/django-oscar/commit/17851d4b66922f8d7e203e2b469040690c84a0db

$ git url head^
https://github.com/tangentlabs/django-oscar/commit/f49d055befc90897c030e0447a98d512cca4265b

Several times a day, I run one of the above, piping the output into the clipboard for easy pasting:

$ git url | clipboard
https://github.com/tangentlabs/django-oscar/commit/17851d4b66922f8d7e203e2b469040690c84a0db

where:

# ~/.bashrc

alias clipboard='pbcopy'  # osx clipboard

Here’s a few more useful git aliases based on the browse subcommand:

# ~/.gitconfig

[alias]
commits = !hub browse -- commits
issues = !hub browse -- issues
wiki = !hub browse -- wiki
settings = !hub browse -- settings
——————

Something wrong? Suggest an improvement or add a comment (see article history)
Tagged with: git, commandlinefu
Filed in: tips

Previous: Continuously rebuild your project
Next: Integrating Django application metrics into Zabbix

Copyright © 2005-2024 David Winterbottom
Content licensed under CC BY-NC-SA 4.0.