Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
git pr - Global .gitconfig aliases for Pull Request Managment

Install

Either copy the aliases from the .gitconfig or run the commands in add-pr-alias.sh

Usage

Easily checkout local copies of pull requests from remotes:

  • git pr 4 - creates local branch pr/4 from the github upstream(if it exists) or origin remote and checks it out
  • git pr 4 someremote - creates local branch pr/4 from someremote remote and checks it out
  • git pr-clean - removes all pr/* branches from your local repo
  • git spr - Same as git pr command, but for bitbucket/stash remotes
# for github remotes
[alias]
pr = "!f() { git fetch -fu ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f"
pr-clean = "!git for-each-ref refs/heads/pr/* --format='%(refname)' | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done"
# for bitbucket/stash remotes
spr = "!f() { git fetch -fu ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull-requests/$1/from:pr/$1 && git checkout pr/$1; }; f"
#!/bin/sh
# For github
git config --global alias.pr '!f() { git fetch -fu ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f'
git config --global alias.pr-clean '!git for-each-ref refs/heads/pr/* --format="%(refname)" | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done'
# For stash/bitbucket
git config --global alias.spr '!f() { git fetch -fu ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull-requests/$1/from:pr/$1 && git checkout pr/$1; }; f'
@gnarf
Owner
gnarf commented Apr 23, 2013

Added pr-clean to delete pr/* branches at request of @scottgonzalez

@gnarf
Owner
gnarf commented Apr 24, 2013

Added prr to grab from remote, i.e. prr upstream 55

@gnarf
Owner
gnarf commented Sep 3, 2013

Added pru to grab from "upstream" in case you have that branch name (pretty common)

@whit537
whit537 commented Sep 3, 2013

I like these as bash aliases rather than git aliases. Here's my adaptation (in ~/.bashrc):

pr() {
    git fetch -fu origin refs/pull/$1/head:pr/$1
    git checkout pr/$1
}                                                                                                           
prc() {                                                                                                         
    git for-each-ref refs/heads/pr/* --format='%(refname)' | \
        while read ref; do git branch -D ${ref#refs/heads/}; done
}
@FichteFoll

Exactly what I needed.

@andreineculau

Thanks. I have removed the "noise" (pr, pru, prr) though:

pr = !git fetch -fu ${2:-origin} refs/pull/$1/head:pr/$1 && :

so now you can

git pr 123
git pr 123 upstream
@mszczepanczyk

This is great. Thanks!

@tzz
tzz commented Jan 6, 2014

nice, thanks!

@rstacruz

I've combined @whit537 and @andreineculau's suggestions:

[alias]
pr  = "!f() { git fetch -fu ${2:-origin} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f"

...now you can do git pr 482 upstream and switch to that branch immediately.

@gnarf
Owner
gnarf commented Aug 26, 2014

@rstacruz - cheers - updated the gist to reflect your version, I like the bash defaulting - neat trick I always forget I can use :)

@rstacruz
rstacruz commented Oct 7, 2014

Hey @gnarf, I've opened a pr for this in git-extras: tj/git-extras#262

@Stibbons

So cool !

@bdowling

Building on the building (and lazy defaults), since a lot of people use an upstream remote and that's where PRs are often worked... :)

pr  = "!f() { git fetch -fu ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f"
@gnarf
Owner
gnarf commented Feb 17, 2017

@bdowling makes sense!

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