How can I fetch an unmerged pull request for a branch I don't own?

Viewed 67519
13 Answers

To fetch a pull into your repository:

git fetch git@github.com:jboss/jboss-common-beans.git refs/pull/4/head

Then do whatever you want with FETCH_HEAD:

git checkout -b new-branch FETCH_HEAD

Here're the commands that worked for me.

I'll assume that one has already cloned a repo (for e.g. pytorch ) to his/her system locally. After that some volunteer/enthusiast has contributed some code and issued a PR to the remote repository but it has not been merged into the master or any other branch yet. So,

First we'll have to do git remote add to the github remote repository:

# I've given the name `original`; you can give some other name as per your liking
$ git remote add original https://github.com/pytorch/pytorch

Then cd into the repository pytorch and then simply do:

# after this, the unmerged PR should be pulled to your local repo
$ git fetch original pull/<pull_number>/head    # 23, 123 etc.,

Now, the pending PR has been fetched into your local repo and the tip of your fetch would be in FETCH_HEAD. If you want to merge this pending PR locally, then simply do:

$ git merge FETCH_HEAD

After this, if you do:

$ git status

You should be able to see that the local repo is ahead of n commits that were part of the pending PR (i.e. it's possible to issue more than 1 commit in a single PR). So, the number of commits depend on the commits contained in the pending PR.

This is the solution from GitHub docs:

From your project repository, check out a new branch and test the changes.

git checkout -b GithubUserID-branchName branchName
git pull https://github.com/GithubUserID/reponame.git branchName

Where:

  1. GithubUserID is the username of the person who opened the pull request.
  2. branchName is the name of the branch for example master
  3. reponame the repository name like demo-todo

Example :

git checkout -b felix123-master master
git pull https://github.com/felix123/demo-todo.git master

If you just want to add one unmerged Pull Request from some other repo, to your own, there is no need for all complications (as mostly shown in other answers).

Instead just go into your own repo and pull in the commit (from PR source), using its commit hash.

git pull https://bitbucket.org/SomeUser/SomeProjectRepo/commits/c15...db2

Doing it this way, you will just have a bunch of new edited files, as if you had edited them yourself. It's then up to you if you want to commit these with some tag/label.

If you then want to push all news up to your own GitHub repo, just do as always:

git commit -m "Added something by Anonymous"
git push -u origin master

Github has clear doc for merging the pull request into a local repo:

https://help.github.com/en/articles/checking-out-pull-requests-locally

It boils down to knowing that a pull request in GitHub is just a branch in the base repo where the branch name is a sequence number. The above article shows you how to find this number and the git command line magic to pull it into your local repo with whatever branch name you'd like.

I could not find a similarly simple way to merge the pull request into a fork I created on GitHub.

I found this solution to this problem - pulling changes from unmerged PR on different Machine , I did following things on git bash 1. You must have taken clone of remote repository on machine 2 2. do git checkout (branch on which PR was generated) 3. do git pull and done!!!!!!!!!!!!!!

Related