How to Update a Forked Repository
Forking
means you copied it to your own but was tagged as forked from the original repository. You can add, edit and delete files in your version.
Your forked repository could fetch easily from the upstream.
Update a Forked Repository
in Git
Cloned your forked repository, go to your project folder, and run the following command on your bash.
git remote add upstream https://github.com/authorname/original-repository-name.git
This code is to add a remote called upstream
. The remote
is like a nickname of the link of repositories.
It’s like the remote origin
when you do git fetch origin master
. You can now do a git fetch upstream master
.
git remote -v
origin https://github.com/you/original-repository-name.git (fetch)
origin https://github.com/you/original-repository-name.git (push)
upstream https://github.com/authorname/original-repository-name.git (fetch)
upstream https://github.com/authorname/original-repository-name.git (push)
Now, we will fetch all the upstream branches.
git fetch upstream
Select your branch in which you were to update your changes for your forked repo. For example, master
.
git checkout master
Merge the branch from the upstream. The history of your commits won’t be affected in this case.
git merge upstream/master
If you want a full clean from the upstream, run the following code instead. This will rewrite your commits and may affect those who cloned your repo.
git rebase upstream/master
For the last step, push your branch.
If you did merge, run the following code.
git push origin master
If you did rebase, run the following code.
git push -f origin master
Note that the force -f
flag is only required at the first push after you’ve rebased.
Reconsider Facts on Updating a Forked Repository in Git
Reconsider those who’ve cloned your repository against the upstream
and your time, work, and efforts.
When you’re unsure about what you’re pulling or updating, always create a branch.
Do you have a backup repository? Do you do branching? It’s easier to remove a temporary branch than reworking on the stuff you already did.
So before applying this to your actual repo, practice this tutorial first to get familiar with the steps and get used to what works and whatnot.