Skip to content

Instantly share code, notes, and snippets.

@hjdivad
Created September 2, 2013 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjdivad/6417895 to your computer and use it in GitHub Desktop.
Save hjdivad/6417895 to your computer and use it in GitHub Desktop.
# ~/.gitconfig

[alias]
  # "fetch origin"
  fo = fetch --prune origin
  # "checkout pull request"
  cpr = "!f() { git checkout -b pull/$1 pull-requests/$1; }; f"

[remote "origin"]
	fetch = +refs/pull/*/head:refs/pull-requests/*
# ~/.bashrc
#
# This assumes you will source git-completion.bash
# https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
#
# Please note you must create these functions *before* sourcing
# git-completion.bash, at least if you want stuff to actually work.

__git_pull_refs() {
	local dir="$(__gitdir)"
	if [ -d "$dir" ]; then
		git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
			refs/pull-requests | sed 's/^pull-requests\///'
		return
	fi
}

# _git_<cmd> is checked for completing git command <cmd>
_git_cpr() {
  __gitcomp_nl "$(__git_pull_refs)"
}

Now when you fetch from origin you'll pick up pull-request refs if origin is a github remote. You can then git cpr <n> to check out pull request <n>, and even get bash completion.

See this asciicast for a trivial demonstration.

Todo

  • It would be a big improvement if we could get a refspec that only included open pull requests and easily prune them on fetch.
@hjdivad
Copy link
Author

hjdivad commented Sep 3, 2013

this also assumes sed is in your $PATH

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