Skip to content

Instantly share code, notes, and snippets.

@scrogson
Last active November 14, 2017 13:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scrogson/5093330 to your computer and use it in GitHub Desktop.
Save scrogson/5093330 to your computer and use it in GitHub Desktop.
Shell functions to aid in using hub(1) with GitHub Enterprise.

I recently started working at Blue Box and we use GitHub Enterprise (GHE) for all of our apps. I had gotten in the habit of using hub to improve my git workflow and was curious if I could use hub with GHE. I turned to the docs and sure enough, you can!

The docs tell you two ways of setting up hub to use GHE:

By default, hub will only work with repositories that have remotes which point to github.com. GitHub Enterprise hosts need to be whitelisted to configure hub to treat such remotes same as github.com:

$ git config --global --add hub.host my.git.org

The default host for commands like init and clone is still github.com, but this can be affected with the GITHUB_HOST environment variable:

$ GITHUB_HOST=my.git.org git clone myproject 

I want to use both github.com and GHE, so I decided to create a couple of shell functions to save my fingers from all of that extra typing.

Workflow

Clone a repo (probably your fork of the master repo)

$ ghe clone <username>/<repo>

Setup the repo to use GHE by default

$ ghe-setup

NOTE: Be sure to alias hub as git ;)

# example with zsh
hub_path=$(which hub)
if (( $+commands[hub] ))
then
  alias git=$hub_path
fi

Set up the master branch to track the master repo that everyone will send pull requests to

$ git remote add <remote-name> <username>/<repo-name>

Fetch the remote

$ git fetch <remote-name>

Set up the main repo master branch as the upstream for local master

$ git branch -u <remote-name>/master

Code. Commit. Push.

Submit a pull request

$ git pull-request -b <remote-name>:master

There's so much more you can do with hub, so be sure to check out the docs:

$ man hub
GITHUB_ENTERPRISE_DOMAIN=github.example.com
# Allows you to use hub(1) with your GitHub Enterprise
# account without setting it globally in your ~/.gitconfig.
# Example: `ghe clone username/repo`
function ghe() {
GITHUB_HOST=$GITHUB_ENTERPRISE_DOMAIN git $*
}
# Setup an existing repo to use GitHub Enterprie exclusively
function ghe-setup() {
git config --add hub.host $GITHUB_ENTERPRISE_DOMAIN
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment