How to switch to the branch of a pull request with Git

452 views Asked by At

How do you "switch to" or "checkout" the branch that's the source of a pull request (especially in the eGit Eclipse plugin)?

Since I mostly use Git for personal projects, I'm still a little new to some of its non-centralized features. I manage a few small open source projects on Github, and usually the pull requests are trivial, so it's simple enough to visually review and then merge the pull request and verify locally by running unittests before I publish elsewhere.

However, for larger more complicated pull requests, I'd like to checkout the pull request's branch (or "switch to" it in my local project directory so I can re-use my existing virtualenv), and inspect it more thoroughly before I merge the pull request on Github. How would I do that?

2

There are 2 answers

1
Dave Zych On

Add the user's fork as a remote, fetch and perform a checkout of the source branch of the pull request.

git remote add {remotename} http://github.com/url/to/users/repo
git fetch {remotename}
git checkout {pull-request-branch-name}

From there you can run the code, run the unit tests, git diff master or do whatever you need to do.

0
Johannes Ewald On

I've written a small git custom command: Place this git-pr file somewhere in your $PATH and then you can just write:

git pr 42
// or
git pr upstream 42
// or
git pr https://github.com/peerigon/phridge/pull/1