Published: 19.04.2021 | Edited: 09.05.2021 | Tags: 100daystooffload,git

Keep Git fork in sync

Steps below explain to keep the forked version up to date with the upstream branch of a forked repository. I know this was already documented many times, but I was struggling with it for some time, until I have found the workflow that suits me the best, so I documented it.

Create a fork in the UI, clone the forked repository and change directory:

git clone --recurse-submodules git@github.com:peterbabic/a-forked-repository.git
cd forked-repository

Add the upstream remote, do this only if remote is not already present:

git remote add upstream git@github.com:ORIGINAL-ACCOUNT/repository.git

Get back to the main branch, if not already there:

git checkout main

Fetch latest changes and add them to the repository:

git pull --rebase upstream main

Push the changes back into the origin (the fork), --force may be needed:

git push

Repeat last three commands to keep the forked repository updated.

Tip: It is possible to skip writing --rebase with this setting:

git config --global pull.rebase true

For the completeness, here's how remotes should look like:

git remote --verbose
origin	    git@github.com:peterbabic/a-forked-repository.git (fetch)
origin	    git@github.com:peterbabic/a-forked-repository.git (push)
upstream    git@github.com:ORIGINAL-ACCOUNT/repository.git (fetch)
upstream    git@github.com:ORIGINAL-ACCOUNT/repository.git (push)

This is a 40th post of #100daystooffload.

Links