check out a “pull request” on Git to review/test a PR

We have a repo that I have added to my local fork as Upstream. Someone sent a PR and I want to review it. Although I’m able to review code on Github by their Diff tool but I want to have it on my local machine and test it on a real device in order to make sure it works fine (Android repo it is).

What is best way to checkout this PR?

  • I deleted the known_hosts file from .ssh folder and I can't pull or push in BitBucket repositories
  • Git add . failing on subdirectory that has a .git folder, even though being ignored
  • Should I clone a node library inside node_modules to modify it?
  • EGit - “Replace With Commit…” doesn't work as excepted
  • Git reverting old version as new commit across file name changes
  • How to deal with merges of topic branches after a squashed merge of parent
  • Reverting a broken merge and reapplying selected commits with Git
  • What is a fix for manually renamed files in a git project?
  • Git : fatal: exec hooks/post-receive failed
  • Change Git repository directory location.
  • git default remote branch with gitolite
  • Git lfs (Large File Storage) says lfs managed files are modified after a git lfs pull
  • 3 Solutions collect form web for “check out a “pull request” on Git to review/test a PR”

    It’s somewhat underdocumented, but GitHub provides pull requests as branches on your Upstream repository so you don’t need to add remotes for each third party who might send you a pull request.

    So

    git fetch Upstream pull/1044/head:pr1044-from-someone
    

    will make the commits in the pull request available in your local repository in a new branch, pr1044-from-someone, which you can review and merge locally as appropriate.

    See help.github.com’s Modifying an inactive pull request locally for more details.

    Another option is

    • add the PR’s repository as a remote in your local copy

      git remote add remote_name github.com/user/proj
      
    • fetch changes from this remote

      git fetch remote_name branch_name_to_fetch
      
    • checkout the changes using

      git checkout remote_name/branch_name_to_fetch
      

    And now you can test it as per your needs on your local, you can create new branch, take a diff between branches, and so on.

    PS: IMO, this is slightly tedious compared to your current approach – if you get a lot of pull requests, adding all the remotes can be confusing and fetching all the branch history is sub-optimal, a patch is perfect.

    I just figured it out how to do that. Let me explain what I did for everyone how has same question.

    1. Goto PR page on Github. The URL should looks like https://github.com/YOUR-NAME/REPO-NAME/pull/1044/files
    2. Add .patch at the end of URL and hit Enter so the URL looks like https://github.com/YOUR-NAME/REPO-NAME/pull/1044/files.patch
    3. Download this file or if is not downloadable create a text file, copy page and paste it into text file. Make sure the extension file is .patch (not .txt)
    4. Create a new local branch and checkout into that branch
    5. Take a look at what changes are in the patch by following command. This is just stat and doesn’t apply anything: git apply --stat fix_empty_poster.patch
    6. By following command you will see how close to trouble you are 🙂 If there is no complain be happy: git apply --check fix_empty_poster.patch
    7. Finally you can patch it by following command: git am --signoff < fix_empty_poster.patch

    Now, you have a copy of branch for review.

    More references:

    1. GitHub Tip: download commits as patches
    2. How to create and apply a patch with Git
    Git Baby is a git and github fan, let's start git clone.