Useful Git Commands: a Shortcut for GitHub Pull Requests

3 Nov 2015

Normally, when someone asks me to merge something in git I need to add his or her repository using git remote add, fetch the branch I need, and then merge it. When someone submits a pull request to a project hosted on GitHub, however, GitHub additionally publishes it as something I can fetch from my own repository:

% git ls-remote origin | grep pull/6
03d7fb7af91a74bb7658a0742fa68bfeb5d50a3f	refs/pull/6/head
f8ebbc62555143019947f1255b064cda38fd239f	refs/pull/6/merge
8a42021dbabcfc222b4d47c5ada47e344154d934	refs/pull/60/head
f4d8052000601e59e4e7d4dec4aa4094df4e39a0	refs/pull/61/head
8a519986f5b59721692ec75608edf0f404f88e87	refs/pull/62/head
9c03e723e8b50ca56a1257659d686a68a69e6e40	refs/pull/62/merge
f3a983c6fc3ff236c2bc678cfec3885da609f79a	refs/pull/63/head
e7953c21a77fb37fb7158dd87fc0c156dd8f97ae	refs/pull/63/merge

With this I can use one line of configuration to create a convenient shortcut that lets me immediately check out any pull request:

% git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pull/*"
% git checkout pull/63
Branch pull/63 set up to track remote branch pull/63 from upstream.
Switched to a new branch 'pull/63'