Every day working with git we create Pull Requests (PRs). This process is the bread and butter of collaboration with git: forking a repository, creating a branch to implement a fix and then sending a PR to the maintainer. For example see the GitHub Docs: Creating a pull request from a fork.

The other day I came across a slightly different case - contributing to someone else’s in-progress pull request. They had a branch open on their own fork of a project - and the change I wanted to send was for content only on that branch. Git supports this process (in fact the every-day case is just a specific example where the PR is to the core repository and the target branch is main). GitHub also provides tools to support this process, but the workflow is a little different from the every-day case because both the base and the head of the PR need to be set away from their usual defaults.

An example scenario

  • There is a repository projectuser/example-repo - and both myuser and otheruser are contributors and have forked their own repos.
  • On otheruser’s fork of example-repo they have a branch fix-ticket-NNN that is the subject of an open pull request.
  • I want to suggest a change to otheruser’s fix-ticket-NNN branch such that it will contribute directly (and easily) to their existing PR.

Worked example

This example demonstrates building a PR from myuser branch fix-ticket-NNN-followup into otheruser’s branch fix-ticket-NNN.

# Clone my fork of the repo
git clone git@github.com:myuser/example-repo.git
cd example-repo

# Fetch otheruser's branch into a local branch 
git fetch git@github.com:otheruser/example-repo.git fix-ticket-NNN:fix-ticket-NNN-followup
git switch fix-ticket-NNN-followup

# Make local changes on the local branch / test / etc. ... and then commit + push
# Reminder: origin in this case is myuser's fork
git add .
git commit -s
git push -u origin fix-ticket-NNN-followup

When the push is run, the response may include a helpful comment and URL for creating a new PR. If you miss it you can open the fix-ticket-NNN-followup branch on the website to create one. This is a bit more involved than usual because the repositories and branches involved need to be set away from their defaults.

For the example above, make sure that:

  • base repository is set to the other person’s fork (eg. otheruser/example-repo)
  • base branch is set to the other person’s branch that I patched (eg. fix-ticket-NNN)
  • head repository is set to my repo (eg. myuser/example-repo)
  • compare branch is set to my branch (eg. fix-ticket-NNN-followup)

If all has gone well, GitHub should show these as mergeable and allow the PR to be created.

If the PR is then reviewed and picks up comments or suggested changes GitHub also offers a couple of handy options:

  • Using the website Suggested changes can be committed directly to the branch on the PR (and can even be batched together into a single commit from the “Files changed” view).
  • Changes can also be made on the local repo branch and pushed as above. These will automatically appear in the PR to otheruser’s branch, since the PR tracks the latest state of that branch.

Compare-URL shortcut

Note a short-cut to build the PR is to use a GitHub compare url. Using the example scenario the compare URL would look like:

https://github.com/otheruser/example-repo/compare/fix-ticket-NNN...myuser:example-repo:fix-ticket-NNN-followup

This is GitHub’s compare view. Visiting it shows a diff between otheruser’s fix-ticket-NNN branch and my fix-ticket-NNN-followup branch, and gives a “Create Pull Request” button pre-filled with the right base and head. It saves clicking through the PR form’s dropdowns to select the fork and branch manually.