Skip to content

Instantly share code, notes, and snippets.

@scmx
Last active July 1, 2017 10:56
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 scmx/9943d3b84069fa38f69b21c70db96af0 to your computer and use it in GitHub Desktop.
Save scmx/9943d3b84069fa38f69b21c70db96af0 to your computer and use it in GitHub Desktop.
Bash function for checking out a github pull request branch #git #github #pull-request #bash

Are you having trouble checking out someone's pullrequest from a terminal?

Example usage

git clone git@github.com:example/example.git
ghpr someuser:somebranch # Copy this from the PR

It does these things

  • Adds someuser as a git remote
  • Fetches from someuser remote
  • Checks out the branch somebranch
ghpr() {
  local user=$(echo ${1:?} | cut -d \: -f1)
  local branch=$(echo ${1:?} | cut -d \: -f2)
  local repo=$(basename "$PWD")
  git remote add "$user" "git@github.com:$user/$repo.git"
  git fetch "$user"
  git checkout "$branch"
}

Alternatives

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