Skip to content

Instantly share code, notes, and snippets.

@ecampidoglio
Created October 30, 2019 12:19
Show Gist options
  • Save ecampidoglio/c06a87e4bba905c26e1478b50d88e793 to your computer and use it in GitHub Desktop.
Save ecampidoglio/c06a87e4bba905c26e1478b50d88e793 to your computer and use it in GitHub Desktop.
Checkout Pull Request

Checkout Pull Request

A Git alias to quickly check out a Pull Request branch in a single move. ⚡

git config --global alias.cpr '!f() { git fetch --quiet ${GIT_DEFAULT_REMOTE-origin} pull/$1/head:pulls/$1 && git checkout pulls/$1; }; f'

Usage

Once you have defined the alias, you can simply say:

git cpr 42

Git will fetch the pull/42 branch from the remote named origin and create a local branch called pulls/42. Then, it will switch the working copy to that branch — as simple as that.

Configuration

If you want to work with a remote named something other than origin, you can define the default remote name in the GIT_DEFAULT_REMOTE environment variable:

export GIT_DEFAULT_REMOTE=<remote-name>

or in PowerShell:

$env:GIT_DEFAULT_REMOTE = "<remote-name>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment