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"`'

Edit by ches: After much fussing, here’s my version that will honor the currently active branch, or fall back to master if there is not a matching remote branch. You cannot actually break the line with backslashes as shown here simply for clarity (I’d suggest copying from the raw gist). As suggested by others, modify the OS X-specific open and sed -E with what is appropriate for you. If you see a simpler way to do this, do share.

http://gist.github.com/90083

alias github='br=$(git branch --contains HEAD | sed -En "s/^\* //p"); \
    if ! git ls-remote . | grep -q -e "refs/remotes/.*/${br}"; then br="master"; fi; \
    echo $(git config -l | sed -En "s%remote.origin.url=git(@|://)(github.com)(:|/)(.+/.+).git%https://\2/\4/tree/${br}%p")'

Edit by ssokolow: Thanks to the Portland project, modern Linux has an equivalent to the open command named xdg-open. I’ve forked ches’s version of the alias to replace “open” with “xdg-open” and “-En” with “-rn”. Due to the linebreaking issues, I’m just providing a gist link.

http://gist.github.com/135000

Last edited by ssokolow, 11 days ago
Versions: