Home | Edit | New

Guides: Jump to github from your repository feed

Overview

Run this simple command in a local git repository and it will open your browser to the relative github project page (if the repository was cloned from github). Add it to your ~/.bash_profile file.

http://pastie.caboo.se/158400

alias github="open `git config -l | grep 'remote.origin.url' | sed -n 
's/remote.origin.url=git@github.com:\(.*\)\/\(.*\).git/https:\/\/github.com\/\1\/\2/p'`"

If you’re interested in working on tasty command line nuggets like the one above, please check out the GitHub gem.

Edit by savetheclocktower: To get this to work, I had to escape the backticks so that the command within the backticks would not be evaluated until each time the alias is run. Without the backslashes, the command runs as soon as the alias is declared, at which time (at least in my case) it fails to find a URL.

alias github="open \`git config -l | grep 'remote.origin.url' | sed -n
's/remote.origin.url=git@github.com:\(.*\)\/\(.*\).git/https:\/\/github.com\/\1\/\2/p'\`"
Edit by mkramlich: That didn’t fix it for me either, still broken, but got it closer. I also had to adjust the pattern to match the git URL syntax actually used in my remote.origin.url value. It now works perfectly for me as originally advertised, like this:
alias github="open \`git config -l | grep 'remote.origin.url' | sed -n
 's/remote.origin.url=git:\/\/github.com\/\(.*\)\/\(.*\).git/https:\/\/github.com\/\1\/\2/p'\`"

Edit by griff: Here is one that combines the two above versions and therefore supports both git@github.com and git://github.com urls.

http://pastie.caboo.se/202275

alias github="open \`git config -l | grep 'remote.origin.url' | sed -En \
  's/remote.origin.url=git(@|:\/\/)github.com(:|\/)(.+)\/(.+).git/https:\/\/github.com\/\3\/\4/p'\`"

Edit by FiXato: Here is one that takes griff’s above version and makes it use ‘konqueror’ instead of open (as that command seems to be missing on my system), and runs it backgrounded, with all commandline output surpressed. It also uses sed -r instead of sed -E, as the -E argument was not supported by my version of sed (‘GNU sed version 4.1.5’).

alias github="konqueror \`git config -l | grep 'remote.origin.url' | sed -rn \
  's/remote.origin.url=git(@|:\/\/)github.com(:|\/)(.+)\/(.+).git/https:\/\/github.com\/\3\/\4/p'\` > /dev/null 2>&1 &"

Edit by Leonid Volnitsky: Yet another version(shorter and cleaner). And yes, open is non standard, as is -E (deprecated?):

alias github='firefox `git config -l | 
sed -rn "s^remote.origin.url=git(@|://)(github.com)(:|/)(.+/.+).git^https://\2/\4^p"`'

Clarification by jacaetevha: The open command and the sed -E argument are Mac (BSD) specific. So, if you want this to work on your Mac, keep them in there. open is a commandline program to use the default application for the argument passed to it (filename, URL, etc).

Edit by jacaetevha: Here’s a version I use at work with a repository that started internally and then we push to Github. The difference here is that the origin is not Github, so the RegEx needs to account for that. It also works when Github is the origin.

alias github='open `git config -l | grep github | grep url | \
    sed -En "s^(remote.*.url)=git(@|://)(github.com)(:|/)(.+/.+).git^https://\3/\5^p"`'
Last edited by jacaetevha, 11 days ago
Versions: