Blog Archives

How to download a PR from GitHub to your computer

This is a quick tip about a useful Git technique. It took me a while to figure this out when I first needed it. I was working on a pull request (PR) on one computer when I was in the office. Then I wanted to continue working on the PR from my laptop at home. I needed to transfer my work from my work computer to my laptop, using GitHub as middleman.

Another scenario for this technique is when you’ve used the GitHub UI to make some changes, but now you want to swap to command-line usage while in the middle of your PR. This could be useful, for example, if you find that your PR needs to include changes to more than one file, which is hard to do in the GitHub UI.

Prerequisites

You need Git on your local computer. See the Git installation guide.

I’m assuming the following things:

  • You’re comfortable using command-line Git.
  • You already have a PR that you’ve been working on, and you want to make a local copy of the PR so that you can update one or more files in that PR. (If you haven’t yet created a PR, you can follow this quick guide to working on GitHub, which I created for the Kubeflow open source doc set that I’m currently working on.)
  • You’ve pushed your latest changes up from your other machine to GitHub, so that GitHub contains the latest version of the PR.

All you want to do now is to copy a particular PR down from GitHub so that you can work on it on this computer.

Clone the repository to your computer

If you’ve already cloned the GitHub repository to your local computer, you can skip this section. This would be the case if you’ve previously done some work on this repository and on this computer.

You need a clone of the GitHub repository on the computer you’re currently using, so that Git can track the changes you make in the repository. Usually, you fork the main repository on GitHub before creating a PR. The reason for creating the fork is that you probably don’t have update rights on the main repository. I’m assuming that you have a fork of the repository, and therefore your next step is to clone your fork of the repository to your local computer, as described below.

Note: If you’re working directly on the main repository rather than on your fork of the repository, then you should clone the main repository to your local computer.

To clone your fork of the repository onto your local computer:

    1. Find your fork of the repository on GitHub. For example, if the repository name is “awesome-repo”, then the fork should be at this URL: https://github.com/your-github-username/awesome-repo.
    2. Open a command window on your local computer.
    3. Run the following commands to clone your forked repository onto your local machine. The commands create a directory called git-repositories and then use HTTPS cloning to download the files:
      mkdir git-repositories
      cd git-repositories/
      git clone https://github.com/your-github-username/awesome-repo.git
      cd awesome-repo/

If you prefer, you can use SSH cloning instead of HTTPS cloning:

mkdir git-repositories
cd git-repositories/
git clone git@github.com:your-github-username/awesome-repo.git
cd awesome-repo/

You’re now in a directory called awesome-repo. If you take a look at the files in the directory, you should see some file- and directory names starting with .git, indicating that Git is tracking the files in the directory. You should also see the files and directories belonging to the GitHub repository that you cloned.

Download the PR to your computer

Follow these steps to copy the PR from GitHub to your local computer:

    1. Find your PR on GitHub and check the name of the branch that contains the PR. In the screenshot below, the branch name is gcpsdk:
    2. Go to the directory containing the repository on your local computer. The commands below assume that you’ve cloned the repository into a directory named git-repositories/awesome-repo:
      cd git-repositories/awesome-repo
    3. Run these commands to copy the branch containing your PR to your computer. In the commands, change your-branch-name to the actual branch name:
      git status 
      git checkout master 
      git fetch origin your-branch-name:your-branch-name 
      git checkout your-branch-name

That’s it. You’re now in the branch that contains all the updates from your PR. You can continue working on the files or adding new files. Remember to git commit and git push as usual, to copy your updates back up to GitHub.

Here’s an explanation of each of the above commands:

  • git status: Run this command to see where you are and what the current status is of your files. You may have been busy with something that still needs tidying up before you can create a new branch.
  • git checkout master: Go to the master branch to make sure you have a tidy place from which to create a new branch.
  • git fetch origin your-branch-name:your-branch-name: This is the key command. It tells Git to copy the branch from GitHub (“origin”) and to create a new branch on your local computer with the same updates and the same name.
  • git checkout your-branch-name: This puts you in the new branch on your local computer. This branch now has the same updates as the equivalent branch on GitHub.

Some notes for those who are interested

The above set of commands assumes that you want the branch name on your local computer to be the same as the branch name on GitHub. That’s most likely to be the case, but you can use a different local branch name if you need to. The command pattern is this:

git fetch origin your-origin-branch-name:your-local-branch-name
git checkout your-local-branch-name

The word origin refers to the remote repository on GitHub from which you cloned your local repository when you first started working on it. You can use the following command to see which remote repositories Git knows about:

git remote -v
%d bloggers like this: