debuggable

 
Contact Us
 

Git alias for displaying the GitHub commit url

Posted on 18/3/09 by Felix Geisendörfer

If you often find yourself pointing your team members to commit urls in GitHub, this might be fun for you.

I created git alias called 'hub' that automatically guesses the github repository url for the repository you are currently in:

$ git hub
https://github.com/felixge/my-project

Based on that I created a second alias called 'url', which gives you the url to HEAD commit:

$ git url
https://github.com/felixge/my-project/commit/0bdc57323a1ffec7ffe10bf83147cab5d6838d45

You can however also provide another sha1 you want to link to:

$ git url 22db8914220b717b0954b84365030ae3c9602a17
https://github.com/felixge/my-project/commit/22db8914220b717b0954b84365030ae3c9602a17

If you find those aliases useful, here are my ~/.gitconfig alias definitions for them:

[alias]
  hub =! echo "https://github.com/"`git config remote.origin.url` | sed -E s/[a-z]+@github\.com:// | sed s/\.git$//
  url =!sh -c 'HEAD=`git rev-parse HEAD` && SHA1=`[ "$0" = "sh" ] && echo $HEAD || echo $0` && echo `git hub`"/commit/"${SHA1i}'

(Bash gurus: I am sure you can do the above much more elegantly, wanna give it a try?)

Further I also have this little bash script in ~/bin/tiny:

#!/bin/bash
curl "http://tinyurl.com/api-create.php?url=$1"
echo ""

This allows me to make tiny urls for my links if I need to paste them on long-url unfriendly territory:

$ git url | xargs tiny
http://tinyurl.com/cva44t

Or if you are on OSX, you can use this to open the git url in your browser:

$ git url | xargs open

Now all of the above could probably be done much smarter, but as far as I am concerned it works great ; ).

-- Felix Geisendörfer aka the_undefined

PS: What command line tricks are part of your daily workflow?

 
&nsbp;

You can skip to the end and add a comment.

Nate Abele said on Mar 18, 2009:

"PS: What command line tricks are part of your daily workflow?"

In my ~/.profile:
alias phone='pbpaste | /usr/bin/phone.php'

This command will ssh into my phone and dial the number that's on my clipboard.

Joel Perras said on Mar 19, 2009:

@nate: Now that's just showing off ;-). That's pretty badass.

Tim Koschützki said on Mar 19, 2009:

Pretty neat Nate. Now make me a command that makes me a sandwich.

NOSLOW  said on Mar 19, 2009:

Hey Tim, I almost got it:

alias sandwich='"Hey Mom, make me a sandwich!" | /usr/bin/speaknspell.php'

It only seems to work in my parent's basement, though :/

Jan  said on Mar 21, 2009:

Don't forget to run it with sudo ;)
http://xkcd.com/149/

Binny V A said on Mar 21, 2009:

I have a lot of CLI tricks to help my work - but this is my only git CLI hack - gitstat

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.