Mike F
Mike F

Reputation: 141

Git: How to fetch a pull request for testing

I have been having trouble grabbing a pull request. Here is my command line attempt at the pull request code:

$ git fetch upstream pull/3/head:testbranch
Password for 'http://[email protected]':
fatal: Couldn't find remote ref pull/3/head
Unexpected end of command stream

We are using an origin repo and an upstream repo.

Pull requests go to upstream and origin is just under my name, I would like to pull down a request (#3) for review. However any time I try to grab a pull it gives me the same error even if I change from upstream to origin.

What would be causing this issue? After I grab the code I would like to put it in a branch and basically take over work on it (a hand off) so I don't need access to the person's repo that made the pull request, just the content, and I am set up as an approver for the pull request itself already

Upvotes: 12

Views: 6237

Answers (4)

twk
twk

Reputation: 1841

Install GitHub CLI tools via:

brew install gh

Then, using the PR number from the PR page on GitHub next to the title (has a # sign next to it, e.g, #1234), check it out by running the command:

gh pr checkout 1234

You'll now be on that PR in your editor, and can test locally or look at the line changes in the "Source Control" tab (VS Code for example).

Upvotes: 1

irikefe41
irikefe41

Reputation: 1

Reference example Pull Request

This is pretty straightforward.

First, you need to check your repo remotes using the command.

git remote -v

For the purpose of this example, let's assume your remote is named "origin". Next, you need to retrieve the exact branch name for the pull request as depicted in the sample image.

Finally, you can now checkout the PR using the remote and branch name with the command

git checkout origin/feature/respect-user-corenode

Hope that helps.

Upvotes: 0

Pujan Mehta
Pujan Mehta

Reputation: 498

Looking at the output it seems that either the PR is closed or the PR does not exist.

NOTE: Please commit your code before doing this as if you might have updated the code which will not be present anywhere then you might be in trouble.

The ideal way to test a PR is this way:

git fetch upstream pull/{PR-NUMBER}/head:test-branch-name
git checkout master
git merge test-branch-name

NOTE: If you find out that the code was bad and want to trace back to the previous commit then use this command:

git reset --hard HEAD@{1}

Hope it helps!

Upvotes: 10

mkrufky
mkrufky

Reputation: 3388

Have you tried using the git pull command to pull down the remote branch? You said you want to put the changes in some new branch? For the sake of this answer, let's call it newbranch

First, create the new branch locally:

git checkout -b newbranch

Then, pull down the changes from the upstream testbranch. The following assumes that you are working with a remote called upstream with changes in a branch called testbranch:

git pull . upstream/testbranch

And now you will have created a new branch called newbranch containing the changes in upstream/testbranch.

Upvotes: 1

Related Questions