How to update your PR on GitHub

So, you’ve created a pull request (PR) on GitHub, and you’ve received some review comments. Now you need to update the PR to address the comments. How do you do that? This post shows you how to update the files in an existing PR, using either the command line or the GitHub UI.

When you ask how to update the files in an existing PR on GitHub, the answer is usually something like this:

Just push the updates to the same branch and GitHub takes care of it automagically.

Well, it’s true. GitHub does take care of it, and it is fairly magical. But how do you “just push the updates to the same branch”?

It took me a while to figure that out, the first time I needed to update the files in a PR. You can do it using command-line Git or using the GitHub UI. I prefer the command line, as it’s usually cleaner and simpler in the end, particularly if you need to update more than one file.

First, of course, you need to create the initial PR. If you haven’t yet created a PR, you can follow this guide to working on GitHub, which I created for the Kubeflow open source doc set that I’m currently working on.

I’ll assume that you already have a PR and that you need to update one or more files in that PR. Below are instructions on how to use the command line or use the GitHub UI.

Using the command line to update the files in a PR

Prerequisites:

  • You need Git on your local computer. See the Git installation guide.
  • If you don’t already have a Git repository on your local computer with a branch for your PR, create the local Git repository and branch now. See my blog post on how to download a PR from GitHub to your local computer. This process is necessary if you’ve been working online using the GitHub UI up to now, or if you’ve used a different computer to work on this PR up to now.

Follow these steps to update the PR:

  1. Open a command window on your local computer.
  2. Go to the directory containing the GitHub repository on your local computer. The command below assumes that you’ve cloned the repository into a directory named git-repositories/awesome-repo:
    cd git-repositories/awesome-repo
  3. Tell Git to check out the relevant branch. In this example, replace your-branch-name with the name of your branch:
    git checkout your-branch-name
    
  4. Run git status to check the status of your local files. Make sure the response shows that you’re in the branch that you expect to be in, and that there are no uncommitted changes in your local repository. (It’s a good idea to run git status regularly, just to check that things are as you expect.) If there are uncommitted changes in your local repository, you should take a look at them and consider committing them and pushing them up to GitHub before going any further. The result from git status should look like this:
    On branch your-branch-name
    nothing to commit, working tree clean
    
  5. Run the following command to pull down the most recent changes from your branch on GitHub to your local repository. It’s fine to run this command even if there are no changes to pull:
    git pull origin your-branch-name
    
  6. Update the files that you need to change. You can add, edit, and delete files using your favourite text editor. I’m currently using Visual Studio Code.
  7. Run git status to check the status of your local files. The response should tell you which files you still need to commit for Git tracking. For example, the following status response shows one changed file, named your-updated-doc.md, that you still need to commit:
    On branch your-branch-name
    Changes not staged for commit:
      (use "git add ..." to update what will be committed)
      (use "git restore ..." to discard changes in working directory)
    	modified:   your-updated-doc.md
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    Another example: The following status response shows one deleted file and one added file, both of which you need to commit for Git tracking:

    On branch your-branch-name
    Changes not staged for commit:
      (use "git add/rm ..." to update what will be committed)
      (use "git restore ..." to discard changes in working directory)
    	deleted:    your-old-doc.md
    
    Untracked files:
      (use "git add ..." to include in what will be committed)
    	your-new-doc.md
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
  8. Run the following commands to commit the updated files to your local Git repository. Here’s an example commit command to tell Git about one or more updated files:
    git commit -am "Fixed some doc errors."
    

    Here’s another example, which tells Git that you’ve added a file named your-new-doc.md, and then commits all changes include the added file:

    git add your-new-doc.md
    git commit -am "Added a shiny new doc."
    

    And another example, which tells Git that you’ve deleted a file named your-old-doc.md, and then commits all changes include the file deletion:

    git rm your-old-doc.md
    git commit -am "Deleted an obsolete doc."
    
  9. Push the updates from your local branch to the corresponding branch on GitHub:
    git push origin your-branch-name
    

That’s it. When you look at your PR on GitHub, you’ll see a new commit listed among the comments on the PR. For example, the following screenshot shows two commits on a PR. One commit has the description “Updated for review comments”, and the other has the description “Added instruction to use different deployment name”:

When you view the files in the PR, you’ll see your changes incorporated into the latest version of each file. When the repository maintainers approve your PR, the changes will be merged into the repository.

Some notes:

  • 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

 

Using the GitHub UI to update the files in a PR

There are two ways to update a file in an existing PR using the GitHub UI:

  • Access the file from the PR (described in this section).
  • Access the file from your fork of the repository that you’re updating (described in the section below this one).

The first way is the most direct route to the file that needs updating, once you know how to do it.

To access a file from a PR:

  1. Open a browser window.
  2. Open your PR in GitHub, and click the Files changed tab at the top of the PR:
  3. Click the three dots on the right-hand side of the window next to the name of the file that you want to edit, then click Edit file in the panel that opens up:
  4. Make your changes in the editing interface that opens up.
  5. When you’re ready, scroll down to the bottom of the editing interface. Enter a short description of the updates, and a longer description if necessary. Make sure the option is selected to Commit directly to the your-branch-name branch:
  6. Click Commit changes.

That’s it. You’ve now updated the file in the PR. When you look at your PR on GitHub, you’ll see a new commit listed among the comments on the PR. When the repository maintainers approve your PR, the changes will be merged into the repository.

An interesting point is that other people can also add commits into your PR, provided they have authority to do so. Sometimes the repository maintainers may make an update to your PR before merging it, if it’s simpler to make the update than to explain it in a PR comment. (When you create the PR, you can grant or deny permission for the maintainers to make changes to the PR.) For example, the following screenshot shows a commit that I made into someone else’s PR, before merging the PR into the main repo. The commit has the description “Added specific link to Chainer page”:

 

Accessing your PR from your fork of the repository in the GitHub UI

The above section shows you how to access a file directly from a PR in order to update the file. As an alternative, you can go to your fork of the repository that you’re updating, and navigate through the files in the repository to find the one you want to update.

Usually, you work on a fork of the main repository on GitHub when you create a PR. If you use the GitHub UI to create the PR, GitHub creates the fork automatically for you, the first time you create a PR for a particular repository. The reason for creating the fork is that you probably don’t have update rights on the main repository. In the instructions below, I’m assuming that you have a fork of the repository.

To access a file from your fork of the repository:

  1. Open a browser window.
  2. 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.You can find your fork by going to the list of your repositories on GitHub. Click the dropdown arrow next to your profile image, then click Your repositories:
    You should see a list of repositories something like this:
  3. Click the name of the forked repository that you want to update. You should see the details of the forked repository, including the files within the repository.For example, I clicked website to open my fork of the Kubeflow website repository. The Code tab shows the list of files within the repository:
  4. Change to the branch that contains your PR within the repository fork. By default, the repository fork opens on the master branch. To change the branch, first click the Branch option:
    Then in the dropdown list that appears, select the branch that contains your PR. The branch name may be something like “your-username-patch-1”, or it may be something meaningful that you entered when you created the PR. For example, I need to select the v1-discussion branch to find my PR:
    Now you should see the same repository fork, with roughly the same files as you saw in the master branch. But the branch which you’ve selected contains all the changes you’ve made in your PR.
  5. Click a file or directory name to navigate through the files within your fork of the repository. For example, I need to click the Content directory to find the file I’m interested in:
  6. When you find your file, click the edit icon to edit the file as usual:
  7. When you’re ready, scroll down to the bottom of the editing interface. Enter a short description of the updates, and a longer description if necessary. Make sure the option is selected to Commit directly to the your-branch-name branch:
  8. Click Commit changes.

That’s it. You’ve now updated the file in the PR. When you look at your PR on GitHub, you’ll see a new commit listed among the comments on the PR. When the repository maintainers approve your PR, the changes will be merged into the repository.

About Sarah Maddox

Technical writer, author and blogger in Sydney

Posted on 12 January 2020, in GitHub, open source, technical writing and tagged , . Bookmark the permalink. 1 Comment.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.