MiscMisc

How to update a forked repo from an upstream with git rebase (or merge)

Feb 11, 2020 · Updated: Aug 03, 2020 · by Tim Kamanin

Picture this: you have a fork (origin) of a GitHub repo (upstream). You submitted a PR from origin to upstream and then, upstream contributor says something like this:

"Hey, we've introduced some changes to master, could you please rebase your pull request to the latest master branch?"

Wooooot? How do I do that?!

Calm down, turns out that's pretty simple! Follow me, I'll show.

Ok, let's say the upstream repo URL is https://github.com/django/django.git (yes, yes, you're contributing to Django, great job!)

And your fork repo URL is https://github.com/timonweb/django-fork.git

Now, in order to pull the latest changes from upstream to origin you need to:

  1. Add the upstream remote like this:

    bash git remote add upstream https://github.com/django/django.git

  2. Download master branch from upstream:

    bash git fetch upstream master

  3. Overwrite your master with upstream's master via git rebase:

    bash git rebase upstream/master

  4. Push to master, please note --force here:

    bash git push origin master --force

Is there an alternative?

Yes, it's git merge! There's a lot of debate on git rebase vs git merge. I won't go into much details here, but merge is kinda safer and creates an additional commit that contains merged commits, whereas rebase is good for git log purists since it doesn't create a commit upstream is merged. Rebase is a good choice when no one except you has worked on your feature branch.

I always try to go with the simplest and safest choice, this I prefer merge to rebase, thus if you're like me and want to do the same thing, then merging from upstream would be as simple as:

  1. Add the upstream remote like this:
    bash git remote add upstream https://github.com/django/django.git

  2. Fetch and merge upstream master into your current branch:
    bash git pull upstream master

  3. Push changes back to origin:
    bash git push origin master

And you're done!

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓