Faster PR Pulls with Git Aliases

By  on  

I love the traditional GitHub workflow of receive pull request, pull down pull request to test, and merge the pull request.  GitHub makes the first and the third steps easy but pulling down pull requests from new contributors requires some boilerplate work that annoys me...

...namely navigating to their fork and copying the fork address so that I can do:

git pull https://github.com/some-new-user/repo.git branch-name

And since you don't know if the author will be contributing more in the future, there's no point in adding them as a remote (via git remote add) -- you'll just muddy up your remote listing.

After years of this annoying dance I've found TeamPorcupine's awesome git alias for easily pulling down pull requests:

git config --global --add alias.pr '!f() { git fetch -fu ${2:-upstream} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f'

git config --global --add alias.pr-clean '!git checkout master ; git for-each-ref refs/heads/pr/* --format="%(refname)" | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done'

The first alias above, git pr, allows you to pull down a pull request by ID, creating an new branch for it:

~/Projects/debugger.html (master) $ git pr 4862
From https://github.com/devtools-html/debugger.html
 * [new ref]           refs/pull/4862/head -> pr/4862
Switched to branch 'pr/4862'

~/Projects/debugger.html (pr/4862) $

The second alias, git pr-clean, deletes all branches created with git pr:

~/Projects/debugger.html (pr/4862) $ git pr-clean
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 190 commits.
  (use "git push" to publish your local commits)
husky > npm run -s postcheckout (node v8.5.0)

Deleted branch pr/4862 (was 10fe5049).
~/Projects/debugger.html (master) $

Note: You must have a remote called upstream which points to the upstream repo.

I can't tell you how happy I am to have this pr git alias, and how much of a bonus the pr-clean alias is.  Checking out PRs will be so much faster and I can focus more on code and functional review than process!

Recent Features

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

Incredible Demos

  • By
    Web Audio API

    The Web Audio API allows developers to load and decode audio on demand using JavaScript.  The more I evaluate awesome games for Firefox OS TVs, the more I get to learn about these APIs that I normally wouldn't touch.  The following is a very basic introduction to the WebAudio API...

  • By
    Disable Autocomplete, Autocapitalize, and Autocorrect

    Mobile and desktop browser vendors do their best to help us not look like idiots by providing us autocomplete, autocorrect, and autocapitalize features.  Unfortunately these features can sometimes get in the way;  we don't always want or need the help they provide.  Luckily most browsers allow...

Discussion

  1. I’m using git-extras, I totally recommend it!

  2. “hub” by Github is great for this sort of thing: https://hub.github.com/

  3. hozefa

    How do you then pull new commits added to the PR?

  4. Hi! Thanks for sharing this great git alias!

    One question: why not use origin instead of the upstream remote?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!