Instantly share code, notes, and snippets.

Embed
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

This comment has been minimized.

Show comment
Hide comment
@gnarf

gnarf Apr 23, 2013

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

Owner

gnarf commented Apr 23, 2013

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

@gnarf

This comment has been minimized.

Show comment
Hide comment
@gnarf

gnarf Apr 24, 2013

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

Owner

gnarf commented Apr 24, 2013

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

@gnarf

This comment has been minimized.

Show comment
Hide comment
@gnarf

gnarf Sep 3, 2013

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

Owner

gnarf commented Sep 3, 2013

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

@chadwhitacre

This comment has been minimized.

Show comment
Hide comment
@chadwhitacre

chadwhitacre 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
}

chadwhitacre 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

This comment has been minimized.

Show comment
Hide comment
@FichteFoll

FichteFoll Sep 10, 2013

Exactly what I needed.

FichteFoll commented Sep 10, 2013

Exactly what I needed.

@andreineculau

This comment has been minimized.

Show comment
Hide comment
@andreineculau

andreineculau Sep 25, 2013

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

andreineculau commented Sep 25, 2013

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 comment has been minimized.

Show comment
Hide comment
@mszczepanczyk

mszczepanczyk Oct 12, 2013

This is great. Thanks!

mszczepanczyk commented Oct 12, 2013

This is great. Thanks!

@tzz

This comment has been minimized.

Show comment
Hide comment
@tzz

tzz Jan 6, 2014

nice, thanks!

tzz commented Jan 6, 2014

nice, thanks!

@rstacruz

This comment has been minimized.

Show comment
Hide comment
@rstacruz

rstacruz Jun 11, 2014

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.

rstacruz commented Jun 11, 2014

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

This comment has been minimized.

Show comment
Hide comment
@gnarf

gnarf 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 :)

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

This comment has been minimized.

Show comment
Hide comment
@rstacruz

rstacruz Oct 7, 2014

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

rstacruz commented Oct 7, 2014

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

@gsemet

This comment has been minimized.

Show comment
Hide comment
@gsemet

gsemet Jun 29, 2016

So cool !

gsemet commented Jun 29, 2016

So cool !

@bdowling

This comment has been minimized.

Show comment
Hide comment
@bdowling

bdowling Jan 30, 2017

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"

bdowling commented Jan 30, 2017

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

This comment has been minimized.

Show comment
Hide comment
@gnarf
Owner

gnarf commented Feb 17, 2017

@bdowling makes sense!

@vagkosFoundry

This comment has been minimized.

Show comment
Hide comment
@vagkosFoundry

vagkosFoundry commented Nov 23, 2017

thanks!!

@Gcaufy

This comment has been minimized.

Show comment
Hide comment
@Gcaufy

Gcaufy Dec 25, 2017

If I checkout a pr, and I want to modified it, and then push it back to wait pr author to review it.
How can I do that?

Gcaufy commented Dec 25, 2017

If I checkout a pr, and I want to modified it, and then push it back to wait pr author to review it.
How can I do that?

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