Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git
	fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

Now fetch all the pull requests:

$ git fetch origin
From github.com:joyent/node
 * [new ref]         refs/pull/1000/head -> origin/pr/1000
 * [new ref]         refs/pull/1002/head -> origin/pr/1002
 * [new ref]         refs/pull/1004/head -> origin/pr/1004
 * [new ref]         refs/pull/1009/head -> origin/pr/1009
...

To check out a particular pull request:

$ git checkout pr/999
Branch pr/999 set up to track remote branch pr/999 from origin.
Switched to a new branch 'pr/999'
@dougludlow
Copy link

dougludlow commented Aug 16, 2017

If you want to update a pr/1234 branch locally (resynchronize), you can git pull origin refs/pull/1234/head.

@sduensin
Copy link

sduensin commented Oct 17, 2017

Just another "Thank you"! Very handy!

@xuexb
Copy link

xuexb commented Oct 26, 2017

Thank you~

@gondre
Copy link

gondre commented Nov 7, 2017

Thanks!

@mbzt
Copy link

mbzt commented Nov 11, 2017

Thank you for this. For a reason I don't know I couldn't retrieve only the PR of interest to me, but this approach worked nicely.

@agilewarealok
Copy link

agilewarealok commented Dec 5, 2017

Thanks for this buddy!

@kistters
Copy link

kistters commented Feb 22, 2018

THX

@nishantrayan
Copy link

nishantrayan commented Feb 22, 2018

❤️ it . Github should put it in their official documentation

@IvanMalison
Copy link

IvanMalison commented Feb 25, 2018

Someone should write a script to make this change. Also, this doesn't work for submodules that have a .git directory in a parent directory

@yog3ndra
Copy link

yog3ndra commented May 7, 2018

👍

Copy link

ghost commented Jun 1, 2018

Thank you! 👍

@jeremydouglass
Copy link

jeremydouglass commented Jul 28, 2018

If you want to resurrect a Pull Request that is displaying unknown repository after the fork was deleted then you can do that using the PR checkout gist here.

@aspiers has a comment post about how:

"Pull request displays "unknown repo" after deletion of fork #168"
isaacs/github#168 (comment)

@glensc
Copy link

glensc commented Aug 21, 2018

hi

i created repo containing the script that is able to checkout one specific PR: https://gitlab.com/glensc/git-mr

@craigeddy
Copy link

craigeddy commented Sep 15, 2018

This works for VSTS/TFS as well. The differences:

[remote "origin"]
	url = <repo url>
	fetch = +refs/heads/*:refs/remotes/origin/*
	fetch = +refs/pull/*:refs/remotes/pull/*

and then git checkout --progress --force refs/remotes/pull/119/merge, where 119 is the PR number.

@fierval
Copy link

fierval commented Sep 22, 2018

🥇 Timeless! Thank you!

@Albosonic
Copy link

Albosonic commented Oct 8, 2018

Just for fetching only one pull request. Handy, if you're hacking on something.

git fetch origin pull/7324/head:pr-7324
origin points to the remote server.
pull/7324/head is the remote pull request.
pr-7324 is the local pull-request branch.

👍

@Vehmloewff
Copy link

Vehmloewff commented Mar 25, 2019

WOW! Very handy!
Thanks @piscisaureus

@thiagomgd
Copy link

thiagomgd commented Oct 1, 2019

Cool. But is there a way to checkout the actual result from the PR?
I mean, have the code on the destination (like master) and also the new code from the PR?

@codelady7
Copy link

codelady7 commented Dec 12, 2019

Thanks much - this is exactly what I needed to know! Now I can test a PR locally before merging into the main repo.

@Tas-sos
Copy link

Tas-sos commented May 21, 2020

Very nice hack and discussion!
Thank you all! 🚀

@lingyu77
Copy link

lingyu77 commented Jul 27, 2020

Lovely. Thank you.

@adamkobor
Copy link

adamkobor commented Sep 2, 2020

Lifesaver, thank you <3

@darthwalsh
Copy link

darthwalsh commented Nov 5, 2020

This is a great way to know if a commit is contained by some GitHub PR branch and won't be garbage collected!

Here's an example I went through: in a wiki I pasted several links to github repo files, using a specific hash abc in the URL in case the files was refactoring later. These commits might or might not have been part of some PR where Irebased and force-pushed over the PR branch, so I wasn't sure if this commit was still referenced by some branch, or might someday get deleted.

Running git branch -r --contains abc to search all remote commits didn't show anything, but git branch -r doesn't include all the refs/pull/* so that's not surprising. I could manually checkout relevant commits in git ls-remote and look through the logs to find abc as a parent, but that is error-prone. Making the .git/config change above, git fetch, then running git branch -r --contains abc outputed origin/pr/58 so that answered my question.

@subhamkumar-oyo
Copy link

subhamkumar-oyo commented Dec 20, 2020

I think it's not possible but still confirming if there is a way to check PR's title locally

@darthwalsh
Copy link

darthwalsh commented Jan 8, 2021

@subhamkumar-oyo testing this out, the PR branch only has the commits, and not the PR title. By default the PR title will be the commit title, but that's not guaranteed. Instead, you need to query for the PR title using the GitHub API, which is simple using the gh1 cli:

~/code/FireSocket $ gh pr list --state merged | grep 7.3.1
13	chore(deps): update dependency eslint to v7.3.1	renovate/eslint-7.x	MERGED

@kindlehl
Copy link

kindlehl commented Apr 10, 2021

This is helpful :)

@msaroufim
Copy link

msaroufim commented Apr 27, 2021

❤️

@patgarcia
Copy link

patgarcia commented Dec 23, 2021

A decade later this gist is still super useful. I review homework PRs and this workflow makes that a breeze. Thank you @piscisaureus!!

@yuis-ice
Copy link

yuis-ice commented Feb 2, 2022

Very cool.

@matu3ba
Copy link

matu3ba commented Feb 13, 2022

I had to make a small correction for multiple remotes: git checkout upstream/pr/999, which would be nice to add for completeness.
This is much more compact than the github help page.

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