Rebasing a Fork on Git

There’s tons of resources online about git rebase, but it took me a while to find a solution to this specific problem.

You have a fork of a repository. You’ve made commits on your fork, and there are also new commits on the original repository (call it the "upstream repository"). You want to pull in those changes, but before your commits, so you don’t have a merge.

  1. Add the upstream repository as a remote:
git remote add upstream https://github.com/<upstream user>/<upstream repository>.git
  1. Fetch the upstream changes:
git fetch upstream
  1. Rebase:
git rebase upstream/<branch>
  1. Push, with a force:
git push origin <branch> --force

What if you’ve already commited the merge? Find the hash of the last commit that you made, then run

git reset --hard <hash>

This only works if the merge is the last thing you’ve done; if you have changes on your fork both before and after the branch, it’ll be far more complicated.

This is based off many sources, but primarily this Medium post.

Home | Back to blog

This work is licensed under CC BY-NC 4.0 Creative Commons BY-NC image