Checkout from and push to existing branch in a fork


Context

While working on OSS project, I got a new PR with a contribution from a fork. Before merging, I wanted to push some linting changes on the existing branch in the fork.

Solution

  1. Add the fork as another remote origin:
git remote add contrib <fork_url>
  1. Checkout a new branch (e.g main) that tracks the PR source branch from the fork:
git checkout -b contrib-main contrib/main
  1. After committing my changes, push to the source branch (main in this example):
git push contrib HEAD:main

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.