Get a Pull Request by Number - Bash Function

22 Sep 2021

If you’re working on an open-source project, you’ll find yourself frequently pulling down other people’s PRs. Here’s an easy command to add to your .bash_profile that lets you easily “git” (hehe) a PR by just providing the PR number.

# Add this to your .bash_profile
# Just pass a number (e.g. 451) and the branch will be pulled down for you
# Great for working with lots of forks
gitpr () {
  git fetch origin pull/$1/head:pr/$1 && git checkout pr/$1
}

Usage:

# Call `gitpr` with the number of the PR you're trying to pull down
gitpr 451

# That's it! You'll be switched to branch 'pr/451' and ready to review!