How to update a forked repo from an upstream with git rebase (or merge)
Feb 11, 2020 · by Tim KamaninPicture 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:
Add the upstream remote like this:
bash git remote add upstream https://github.com/django/django.git
Download
master
branch from upstream:bash git fetch upstream master
Overwrite your master with upstream's master via
git rebase
:bash git rebase upstream/master
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:
Add the upstream remote like this:
bash git remote add upstream https://github.com/django/django.git
Fetch and merge upstream
master
into your current branch:bash git pull upstream master
Push changes back to origin:
bash git push origin master
And you're done!