I use the ssh format for adding my remotes, this only seems to pick up http urls.
I hastily redid an else condition that will work with git@github.com: remotes as well.
function GitHub(){if[!-d .git ];then echo"ERROR: This isnt a git directory"&&return false;fi
git_url=`git config --get remote.origin.url`if[[$git_url== https://github*]];then
url=${git_url%.git}else
if[[$git_url== git@github.com*]]then
url="https://github.com/${${git_url:15}%.git}"else
echo"ERROR: Remote origin is invalid"&&return false;fi
fi
open $url}
Just a small & quick modification to add current branch & non-github domain -
alias github=GitHub
function GitHub(){if[!-d .git ];then echo"ERROR: This isnt a git directory"&&return false;fi
git_url=`git config --get remote.origin.url`git_domain=`echo$git_url | awk-vFS="(@|:)"'{print $2}'`git_branch=`git rev-parse --abbrev-ref HEAD 2>/dev/null`if[[$git_url== https://*]];then
url=${git_domain}/${git_url%.git}/tree/${git_branch}else
if[[$git_url== git@*]]then
url="https://${git_domain}/${${git_url#*:}%.git}/tree/${git_branch}"echo$urlelse
echo"ERROR: Remote origin is invalid"&&return false;fi
fi
open $url}
Personally, I prefer this command to be associated with the hub alias, so I can just run:
git hub
And since the folder name is really often the same as the repository name, here's my solution:
git config --global alias.hub '!f () { local REPO;local URL;[[ -f .githubrepo ]] && REPO="$(head -n 1 .githubrepo)" || REPO="${PWD##*/}";URL="https://github.com/<your username>/$REPO";if [[ "$1" == i ]];then URL="$URL/issues";elif [[ "$1" == s ]];then URL="$URL/settings";elif [[ "$1" == p ]];then URL="$URL/pulls";elif [[ "$1" == w ]];then URL="$URL/wiki";elif [[ "$1" == b ]];then URL="$URL/branches";elif [[ "$1" == r ]];then URL="$URL/releases";fi;echo "Opening $URL...";start "$URL";};f'
Make sure you change <your username> to your username.
Now you can run
$ git hub -> opens the repo depending on the folder's name
$ git hub i -> opens issues/
$ git hub p -> opens pulls/
$ git hub s -> opens settings/
$ git hub w -> opens wiki/
$ git hub b -> opens branches/
$ git hub r -> opens releases/
$ git hub whatever -> opens whatever/
If the name of the folder doesn't correspond to the name of the repository, you can add a .githubrepo file and set its content to the name of it, and it'll use this instead.
There's a feature in Git called insteadOf that allows you to make shorthand URLs (so you can say things like git clone me:my-project rather than git clone git@github.com:hoelzro/my-project) - git-config unfortunately doesn't handle insteadOf. If you want your function to work with these remote URLs, you can use git ls-remote --get-url origin instead.
You might also want to use the local bash builtin to introduce git_url and url as local variables to prevent your function from contaminating your bash session!
Cool trick :). I have a bash alias go='git browse' that does the same using hub.github.com, but having the script on the dotfiles would be handy if I'm in a computer where its not available.
Hi guys, this my own version for ssh and https, in one line, also you can run it in a subdirectory:
bash
function GitHub()
{
git_url=`git config --get remote.origin.url`
url=`echo $git_url | sed -e "s/git@github.com:/https:\/\/github.com\//"`
if [[ $url != https://github* ]] ;
then echo "ERROR: Remote origin is invalid" && return false;
fi
# url=${git_url%.git}
# Use "open $url" if you use MacOS
# Use x-www-browser if you use GNU/Linux
x-www-browser $url
}
I've been using git-open and it has worked perfectly for me (even takes you to the current branch). Currently aliased to gh, though it also works with GitLab and Bitbucket.
npm install --global git-open
Bash scripts are great. Just wanted to point out there's a solid implementation of this though for those who want it.
any tip on using two or more remotes (different credentials) on the same machine.. Let's say I use GitHub, my home NAS git and company's git server on the same machine for different projects... How to switch between these fast?
You could just change your ssh config to use different identities based on the IP or something. If you need to use a different email, etc for certain projects you can use git config user.email 'you@email.com' (without --global) to change it from the default global configuration.
I personally just use fugitive from VIM. If I ever need to see something from Github I just run :Gbrowse or if I want to just get the link to share it with someone I run :Gbrowse!.
I use the ssh format for adding my remotes, this only seems to pick up http urls.
I hastily redid an else condition that will work with
git@github.com:remotes as well.Just a small & quick modification to add current branch & non-github domain -
.../bin/git-browse
Nice!
Just for fun: do you have any idea how to add this as a
hubcommand to git? ❤Just put it in a script called
git-huband add that to a directory in your path.Aye! Thanks!
Personally, I prefer this command to be associated with the
hubalias, so I can just run:And since the folder name is really often the same as the repository name, here's my solution:
Make sure you change
<your username>to your username.Now you can run
If the name of the folder doesn't correspond to the name of the repository, you can add a
.githubrepofile and set its content to the name of it, and it'll use this instead.Hopefully it'll save some people a bit of time
There's a feature in Git called insteadOf that allows you to make shorthand URLs (so you can say things like
git clone me:my-projectrather thangit clone git@github.com:hoelzro/my-project) -git-configunfortunately doesn't handle insteadOf. If you want your function to work with these remote URLs, you can usegit ls-remote --get-url origininstead.You might also want to use the
localbash builtin to introducegit_urlandurlas local variables to prevent your function from contaminating your bash session!Cool trick :). I have a bash alias
go='git browse'that does the same using hub.github.com, but having the script on the dotfiles would be handy if I'm in a computer where its not available.Hi guys, this my own version for ssh and https, in one line, also you can run it in a subdirectory:
I've been using git-open and it has worked perfectly for me (even takes you to the current branch). Currently aliased to gh, though it also works with GitLab and Bitbucket.
npm install --global git-open
Bash scripts are great. Just wanted to point out there's a solid implementation of this though for those who want it.
on Linux change
opentoxdg-openor use
x-www-browserin Ubuntuor use
sensible-browserany tip on using two or more remotes (different credentials) on the same machine.. Let's say I use GitHub, my home NAS git and company's git server on the same machine for different projects... How to switch between these fast?
tips needed :) thanks in advance
You could just change your ssh config to use different identities based on the IP or something. If you need to use a different email, etc for certain projects you can use
git config user.email 'you@email.com'(without--global) to change it from the default global configuration.I personally just use fugitive from VIM. If I ever need to see something from Github I just run
:Gbrowseor if I want to just get the link to share it with someone I run:Gbrowse!.Thanks for this. If you have GitHub Desktop installed - you may need to use a different alias.