Skip to content

Instantly share code, notes, and snippets.

@andrewjtait
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save andrewjtait/9114245e92b84d69aa43 to your computer and use it in GitHub Desktop.
Save andrewjtait/9114245e92b84d69aa43 to your computer and use it in GitHub Desktop.
GitHub Bash Function
function github {
branch="$(git rev-parse --abbrev-ref HEAD)"
url="$(git config --get remote.origin.url)"
url=${url/git@github.com:/http://github.com/}
url=${url/.git/}
if [[ $1 =~ "compare" ]]; then action="compare"
elif [[ $1 =~ "pr" ]]; then action="pull"
else action="tree"; fi
if [[ $2 != "" ]]; then base="$2..."
else base=""; fi
url="${url}/${action}/${base}${branch}"
echo "Opening ${url} $(\open ${url})"
}
@andrewjtait
Copy link
Author

This function can be added to your .bash_profile. It supports repositories cloned with SSH, and provides the following commands:

  • github -> opens GitHub repo and branch
  • github compare -> opens GitHub branch comparison with master
  • github compare branch_name -> opens GitHub branch comparison with branch_name
  • github pr -> opens a GitHub pull request against master
  • github pr branch_name -> opens a GitHub pull request against branch_name

@kaelig
Copy link

kaelig commented Nov 5, 2014

Thanks @andrewjtait!

One thing: I had to change if [[ $2 ]]; to if [[ $2 != "" ]]; to make it work (Mac OSX 10.9 using oh-my-zsh).

@andrewjtait
Copy link
Author

Thanks @kaelig, I've updated it with this fix.

@brian-griffin
Copy link

I've added the following between line 10 and 11
elif [[ $1 =~ "pull" ]]; then action="pull"

To allow the following additional commands:

  • github pull -> opens a GitHub pull request with master
  • github pull branchname -> opens a GitHub pull request with branchname

@andrewjtait
Copy link
Author

Thanks @brian-griffin. I've added this, but I've used pr rather than pull as it could be assumed it does something similar to git pull.

@wheresrhys
Copy link

a fork for the lazy typist in me https://gist.github.com/wheresrhys/2bd4ae944003fe57af50. Cheers for producing the original @andrewjtait

@andrewjtait
Copy link
Author

This has now been set up in a repository which is installable through Homebrew: https://github.com/andrewjtait/github-open

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment