-
-
Save igrigorik/6666860 to your computer and use it in GitHub Desktop.
alias gh="open \`git remote -v | grep [email protected] | grep fetch | head -1 | cut -f2 | cut -d' ' -f1 | sed -e's/:/\//' -e 's/git@/http:\/\//'\`" |
Here's what I'm using right now, which has the following advantages:
- It supports both SSH and HTTPS clones of GitHub repos.
- You can specify the file or directory to open in GitHub, otherwise it defaults to the current directory.
- After specifying a file/directory you can also specify the branch to use, otherwise it defaults to opening the current branch.
g-open() {
file=${1:-""}
git_branch=${2:-$(git symbolic-ref --quiet --short HEAD)}
git_project_root=$(git config remote.origin.url | sed "s~git@\(.*\):\(.*\)~https://\1/\2~" | sed "s~\(.*\).git\$~\1~")
git_directory=$(git rev-parse --show-prefix)
open ${git_project_root}/tree/${git_branch}/${git_directory}${file}
}
A few potential improvements for the future:
- Combine and clean up the two
sed
commands. - Support non-GitHub repositores.
- Allow the user to choose an alternative branch by using
--branch
instead of it being determined by parameter location.
Because one more is likely a must :)
function gh() {(
set -e
git remote -v | grep push
remote=${1:-origin}
echo "Using remote $remote"
URL=$(git config remote.$remote.url | sed "s/git@\(.*\):\(.*\).git/https:\/\/\1\/\2/")
echo "Opening $URL..."
open $URL
)}
Tested on MacOS:
- calling
gh
from a non git repo gives you an error message and exists - calling
gh
from a repo... opens it. Works with github, gitlab, etc... - calliing
gh <remote>
opens . For instancegh upstream
.
I developed a small gem giturl
to open github and github enterprise web pages for specified directories. Please try it.
(Sorry, I didn't know igrigorik/github.bash
.)
-
usage
$ giturl -o . https://github.com/shinyaohtani/giturl/lib/
-
how
giturl
works
I used these four git commands.$ git rev-parse --is-inside-work-tree $ git rev-parse --show-prefix $ git rev-parse --abbrev-ref HEAD $ git config --get remote.origin.url
-
installation
$ gem install giturl
-
source code
https://github.com/shinyaohtani/giturl
Just and FYI for Linux users
"open" is native on OS X, on linux you can just define it as alias open=xdg-open,
#Open GitHub from Repo
alias gh="xdg-open \`git remote -v | grep [email protected] | grep fetch | head -1 | cut -f2 | cut -d' ' -f1 | sed -e's/:/\//' -e 's/git@/http:\/\//'\`"
this is a nice tool for doing this kinda stuff https://github.com/paulirish/git-open
i made some adjustments https://gist.github.com/ricardoribas/open-branch
The alias commands above using print need the argument escaped. awk '{print $2}'
should be awk '{print \$2}'
.
Full command here:
alias gh="open \`git remote -v | grep fetch | awk '{print \$2}' | sed 's/git@/http:\/\//' | sed 's/com:/com\//'\`| head -n1"
This one works great for me https://github.com/paulirish/git-open
I recently updated mine to work regardless of whether you are in the current repo. It supports both relative and absolute paths:
#!/bin/sh
## Open file or directory in web browser
## Syntax: git-web $path [$branch]
path=${1:-"."}
if [ -d ${path} ]; then
file_name=""
cd ${path}
else
file_name=$(basename ${path})
cd $(dirname ${path})
fi
project_url=$(git config remote.origin.url | sed "s~:~/~" | sed "s~git@~https://~" | sed "s~\.git~~")
branch=${2:-$(git symbolic-ref --quiet --short HEAD)}
git_directory=$(git rev-parse --show-prefix)
echo ${project_url}/tree/${branch}/${git_directory}${file_name}
open ${project_url}/tree/${branch}/${git_directory}${file_name}
With the new gh cli tool just use: gh repo view --web
If using VS Code, one could also install the GitLens extension (good for other things too) and use the command palette (cmd + shift + P) > GitLens: Open Repository on Remote
With the new gh cli tool just use:
gh repo view --web
even easier, gh browse
@drewbo worked for me on mac, thanks
Edit: Well, I don't actually want to use this, because when I source my .bash_profile
now, I get this as a result:
fatal: not a git repository (or any of the parent directories): .git
Quick and dirty if you already have the vim plugin rhubarb installed:
vim +GBrowse +q
I arrived here looking for a way to convert the github URL into an argument for
go get
. Shamelessly copied from these examples into such a script. https://gist.github.com/lmatter/dd3c95de7155f9450325c3f9c4153fe2