Skip to content

Instantly share code, notes, and snippets.

@karlhorky
Forked from piscisaureus/pr.md
Last active November 2, 2023 13:44
Show Gist options
  • Save karlhorky/88b3c8c258796cd3eb97615da36e07be to your computer and use it in GitHub Desktop.
Save karlhorky/88b3c8c258796cd3eb97615da36e07be to your computer and use it in GitHub Desktop.
Fetch all GitHub pull requests to local tracking branches

NOTE

You may not need local branches for all pull requests in a repo.

To fetch only the ref of a single pull request that you need, use this:

git fetch origin pull/7324/head:pr-7324
git checkout pr-7324
# ...
git branch -D pr-7324

Or, another option is this: Fetch and delete refs to GitHub pull request branches

If you do for some reason need all pull request branches locally, continue on.


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 = [email protected]: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 = [email protected]: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'
@Ju1-js
Copy link

Ju1-js commented Feb 6, 2021

I have already done them manually with a few macros. I'll remember this for later.
Thanks though!

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