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'
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.
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>"