Fetch a remote branch to review changes

Posted on  • tagged with 

When reviewing a PR, its usually helpful to pull into that feature branch and test the changes locally in your own copy of the project.

Can you pull into my feature branch and review the changes locally?

I saw this phrase appear again and again while contributing to Open Source. Before I knew the git commands to accomplish pulling into a feature branch, I tried thinking about how I would actually get the changes from a remote branch. It seemed straightforward, fetch changes from an upstream branch and checkout your own local branch with those fetched changes. Turns out it was really quite simple, but we need to specify an associated pull request ID when fetching the remote branch.

git fetch origin pull/id/head:$BRANCHNAME

We fetch the changes from a specific feature branch and then can use git checkout to switch branches into the feature branch.

git fetch origin pull/2/head:563-some-bug-fix

git checkout 563-some-bug-fix

That's it. Fetch changes from a local or remote feature branch and switch branches to the newly created branch from the fetch. If your pulling into a feature branch from a remote repository, use upstream instead of origin.

git fetch upstream pull/id/head:$BRANCHNAME

Lets say there was a pull request with id equaling 25 and the branch this PR was submitted from is 323-fix-async. The following usage would be:

git fetch upstream pull/25/head:323-fix-async

Then switch branches into the feature branch with git checkout.

git checkout 323-fix-async

Switched to branch '323-fix-async'

Now you have a local copy of the changes in a upstream feature branch, its time to start revewing and approve or reject those changes! If you want to see what branch your currently on, use git branch.

Pulling on a branch

Let's say we had a feature branch named 112-fix-buffer that we submitted some changes on in a public repository called the-buffer-proj. When someone else pulls into your feature branch 112-fix-buffer and makes changes then pushes those changes. Your branch locally will be "behind" as there are changes proposed by others that you haven't synced with yet. To get up-to-date (merge) with your own branch just pull on it to get the latest changes.

git pull origin 112-fix-buffer

This will perform a fetch and merge of remote commits.

Table of contents

Share