Skip to content

Instantly share code, notes, and snippets.

@aurelian
Created May 13, 2011 11:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aurelian/970381 to your computer and use it in GitHub Desktop.
Save aurelian/970381 to your computer and use it in GitHub Desktop.
# Opens the github page for the current git repository and branch in your browser
#
# based on this: http://jasonneylon.wordpress.com/2011/04/22/opening-github-in-your-browser-from-the-terminal/
# + works with current branch
# + pass file: gh foo/bar.txt #=> https://github.com/o/o/tree/master/foo/bar.txt
# + pass commit sh: gh 5116fba #=> https://github.com/o/o/commits/5116fba
#
function gh() {
giturl=$(git config --get remote.origin.url)
if [ "$giturl" == "" ]
then
echo "Not a git repository or no remote.origin.url set"
return 1;
fi
giturl=${giturl/git\@github\.com\:/https://github.com/}
giturl=${giturl/\.git//}
if [ $(git name-rev $1 2> /dev/null | grep -c $1) -eq "1" ]
then
part="commit/$1"
else
part=$(git symbolic-ref HEAD)
part="tree/"${part/refs\/heads\//}"/"$1
fi
echo "$giturl$part"
open "$giturl$part"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment