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 2, 2021

How do I merge all the local pull requests to 'master'?
They are saved like this:

  • [new ref] refs/pull/1/head -> origin/pr/1
  • [new ref] refs/pull/100/head -> origin/pr/100
  • [new ref] refs/pull/102/head -> origin/pr/102
  • [new ref] refs/pull/103/head -> origin/pr/103
  • [new ref] refs/pull/104/head -> origin/pr/104
  • [new ref] refs/pull/105/head -> origin/pr/105
  • [new ref] refs/pull/106/head -> origin/pr/106
  • [new ref] refs/pull/107/head -> origin/pr/107
  • [new ref] refs/pull/108/head -> origin/pr/108
  • [new ref] refs/pull/109/head -> origin/pr/109
  • [new ref] refs/pull/113/head -> origin/pr/113
  • [new ref] refs/pull/114/head -> origin/pr/114
  • [new ref] refs/pull/115/head -> origin/pr/115
  • [new ref] refs/pull/116/head -> origin/pr/116
  • [new ref] refs/pull/117/head -> origin/pr/117
  • [new ref] refs/pull/118/head -> origin/pr/118
  • [new ref] refs/pull/120/head -> origin/pr/120
  • [new ref] refs/pull/122/head -> origin/pr/122
  • [new ref] refs/pull/124/head -> origin/pr/124
  • [new ref] refs/pull/125/head -> origin/pr/125
  • [new ref] refs/pull/128/head -> origin/pr/128
  • [new ref] refs/pull/131/head -> origin/pr/131
  • [new ref] refs/pull/132/head -> origin/pr/132
  • [new ref] refs/pull/137/head -> origin/pr/137
  • [new ref] refs/pull/139/head -> origin/pr/139
  • [new ref] refs/pull/14/head -> origin/pr/14
  • [new ref] refs/pull/140/head -> origin/pr/140
  • [new ref] refs/pull/143/head -> origin/pr/143
  • [new ref] refs/pull/147/head -> origin/pr/147
  • [new ref] refs/pull/15/head -> origin/pr/15
  • [new ref] refs/pull/150/head -> origin/pr/150
  • [new ref] refs/pull/153/head -> origin/pr/153
  • [new ref] refs/pull/155/head -> origin/pr/155
  • [new ref] refs/pull/158/head -> origin/pr/158
  • [new ref] refs/pull/159/head -> origin/pr/159
  • [new ref] refs/pull/16/head -> origin/pr/16
  • [new ref] refs/pull/163/head -> origin/pr/163
  • [new ref] refs/pull/17/head -> origin/pr/17
  • [new ref] refs/pull/173/head -> origin/pr/173
  • [new ref] refs/pull/177/head -> origin/pr/177
  • [new ref] refs/pull/180/head -> origin/pr/180
  • [new ref] refs/pull/184/head -> origin/pr/184
  • [new ref] refs/pull/185/head -> origin/pr/185
  • [new ref] refs/pull/193/head -> origin/pr/193
  • [new ref] refs/pull/198/head -> origin/pr/198
  • [new ref] refs/pull/199/head -> origin/pr/199
  • [new ref] refs/pull/2/head -> origin/pr/2
  • [new ref] refs/pull/200/head -> origin/pr/200
  • [new ref] refs/pull/205/head -> origin/pr/205
  • [new ref] refs/pull/209/head -> origin/pr/209
  • [new ref] refs/pull/211/head -> origin/pr/211
  • [new ref] refs/pull/212/head -> origin/pr/212
  • [new ref] refs/pull/215/head -> origin/pr/215
  • [new ref] refs/pull/217/head -> origin/pr/217
  • [new ref] refs/pull/22/head -> origin/pr/22
  • [new ref] refs/pull/225/head -> origin/pr/225
  • [new ref] refs/pull/226/head -> origin/pr/226
  • [new ref] refs/pull/228/head -> origin/pr/228
  • [new ref] refs/pull/23/head -> origin/pr/23
  • [new ref] refs/pull/234/head -> origin/pr/234
  • [new ref] refs/pull/246/head -> origin/pr/246
  • [new ref] refs/pull/25/head -> origin/pr/25
  • [new ref] refs/pull/257/head -> origin/pr/257
  • [new ref] refs/pull/26/head -> origin/pr/26
  • [new ref] refs/pull/269/head -> origin/pr/269
  • [new ref] refs/pull/27/head -> origin/pr/27
  • [new ref] refs/pull/270/head -> origin/pr/270
  • [new ref] refs/pull/278/head -> origin/pr/278
  • [new ref] refs/pull/279/head -> origin/pr/279
  • [new ref] refs/pull/28/head -> origin/pr/28
  • [new ref] refs/pull/286/head -> origin/pr/286
  • [new ref] refs/pull/288/head -> origin/pr/288
  • [new ref] refs/pull/293/head -> origin/pr/293
  • [new ref] refs/pull/294/head -> origin/pr/294
  • [new ref] refs/pull/295/head -> origin/pr/295
  • [new ref] refs/pull/297/head -> origin/pr/297
  • [new ref] refs/pull/298/head -> origin/pr/298
  • [new ref] refs/pull/3/head -> origin/pr/3
  • [new ref] refs/pull/30/head -> origin/pr/30
  • [new ref] refs/pull/32/head -> origin/pr/32
  • [new ref] refs/pull/33/head -> origin/pr/33
  • [new ref] refs/pull/35/head -> origin/pr/35
  • [new ref] refs/pull/36/head -> origin/pr/36
  • [new ref] refs/pull/37/head -> origin/pr/37
  • [new ref] refs/pull/38/head -> origin/pr/38
  • [new ref] refs/pull/4/head -> origin/pr/4
  • [new ref] refs/pull/40/head -> origin/pr/40
  • [new ref] refs/pull/41/head -> origin/pr/41
  • [new ref] refs/pull/42/head -> origin/pr/42
  • [new ref] refs/pull/43/head -> origin/pr/43
  • [new ref] refs/pull/46/head -> origin/pr/46
  • [new ref] refs/pull/48/head -> origin/pr/48
  • [new ref] refs/pull/5/head -> origin/pr/5
  • [new ref] refs/pull/51/head -> origin/pr/51
  • [new ref] refs/pull/52/head -> origin/pr/52
  • [new ref] refs/pull/6/head -> origin/pr/6
  • [new ref] refs/pull/63/head -> origin/pr/63
  • [new ref] refs/pull/64/head -> origin/pr/64
  • [new ref] refs/pull/68/head -> origin/pr/68
  • [new ref] refs/pull/69/head -> origin/pr/69
  • [new ref] refs/pull/7/head -> origin/pr/7
  • [new ref] refs/pull/78/head -> origin/pr/78
  • [new ref] refs/pull/80/head -> origin/pr/80
  • [new ref] refs/pull/81/head -> origin/pr/81
  • [new ref] refs/pull/85/head -> origin/pr/85
  • [new ref] refs/pull/86/head -> origin/pr/86
  • [new ref] refs/pull/88/head -> origin/pr/88
  • [new ref] refs/pull/89/head -> origin/pr/89
  • [new ref] refs/pull/90/head -> origin/pr/90
  • [new ref] refs/pull/91/head -> origin/pr/91
  • [new ref] refs/pull/92/head -> origin/pr/92
  • [new ref] refs/pull/93/head -> origin/pr/93
  • [new ref] refs/pull/96/head -> origin/pr/96
  • [new ref] refs/pull/97/head -> origin/pr/97
  • [new ref] refs/pull/98/head -> origin/pr/98

@karlhorky
Copy link
Author

Usually I merge on GitHub because it has a few benefits:

  • better communication (shows up in the interface, along with any approvals or request for changes)
  • last time I checked, merging the PR branches on the command line doesn't cause the PR on GitHub to be updated

But if you want to go ahead anyway, I guess this would work (when you're currently on the master branch):

git merge 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