Search code examples
gitgithubversion-controlupstream-branch

Git and GitHub concepts: Reviewing updated pull requests locally


I follow the generic OSS structure:

  1. the OSS' remote repository hosted on GitHub
  2. a fork of the OSS remote repository to my own remote repository
  3. a clone of the fork on my remote repository to create a local repository

Thus, a contributor would create a new branch locally, push the changes to his/her remote repository, and then open a pull request to the OSS' remote repository.

This has been working well. However, the main issue comes when I try to review another contributor's pull request by fiddling with it locally.

So I have fetched a pull request made to the OSS' remote repository using this command:

git fetch upstream pull/<PR#>/head:<branchName>

followed by git checkout <branchName>

and it was successful. I played around with the PR, and reviewed it on GitHub. Then, the contributor updated the PR by pushing new commits to their branch (on their remote repo), which was automatically reflected in the PR.

Now, I want to be able to get the updates locally so that I can try the changes again. I understand that my copy of the PR branch does not track the remote branch by default, so I tried to set it to track the PR:

git branch --set-upstream upstream/pull/<PR#>/head:<branchName>

like how I have done when I first fetched the branch. However, I got the response that

error: the requested upstream branch 'pull//head:' does not exist

I tried again with:

git branch --set-upstream-to upstream/pull/<PR#>/head:<branchName>

which also failed with the same error.

Then, I thought, is it because a PR is like a 'reflection' of the branch on someone's remote repository, so if I want to track an upstream branch I should track from the person's remote repository?

So I added the contributors' remote repository as a remote, and tried again:

git branch --set-upstream-to <newRemote> <branchName>

and I still faced the same error.

I did some Googling, and I found this, but I do not want to get all the pull requests. I also found links like this one but nope, not the help that I need there.

Can anyone point out what is wrong with the way that I am doing things now? Thanks!

Edit: Is there an easier way to do things aside from what has been proposed by Marina Liu - MSFT below?


Solution

  • You can use any of below options to update the PR in local repo.

    Option 1: delete the local source branch and recreate to get the update

    As you use the commands to get the source branch of the PR locally:

    git fetch upstream pull/<PR#>/head:<branchName>
    git checkout <branchname>
    

    If the PR is updated (new commits are pushed to the fork repo), you can delete and recreate by:

    git checkout master
    git branch -D <branchname>
    git fetch upstream pull/<PR#>/head:<branchName>
    git checkout <branchname>
    

    Option 2: add the fork repo as a remote for your local repo

    You can add the fork repo as a remote for your local repo by

    git remote add fork1 <URL for the fork repo> -f
    

    Then you can create a local branch for the PR source branch by

    git checkout -b <branchname> fork1/<branchname>
    

    If the PR has been updated, you just need to execute below commands to get the update:

    git fetch fork1
    git checkout <branchname> #If HEAD is not on the <branchname>
    git reset --hard fork1/<branchname>