Refreshing a Fork

Hi,

you can do that in two ways. Both of them expect that you never work on the default branch (main/master, check your project settings) of your fork, but only create change commits in feature and fix branches.

GitLab Repository Mirror

Navigate into Settings > Repository > Mirroring repositories.

  • Add the upstream URL
  • Mirror direction: Pull
  • Tick Only mirror protected branches (optional)

This feature is available in the Premium tier: Pull from a remote repository | GitLab

Git CLI

Add a secondary remote called upstream.

git remote add upstream https://...

Fetch the remote and then pull its changes into your local default branch, for example main.

git checkout main
git fetch upstream
git pull upstream main

Last, push to your own remote origin to keep the forked repo in sync.

git push origin main

Based on the default main branch, you can continue with creating branches, e.g. git checkout -b feature/....

You could also write a script for that, e.g. when you’re syncing between different Git servers.

One liner script in your forked clone:

git remote add upstream https://gitlab.com/upstream_user_group/project.git || true && git fetch upstream && git checkout main && git pull upstream main && git push origin main

GitLab UI: Fetch upstream history when fork is behind

Update 2023-05-22: Feature available in GitLab 16.0

Feature request in GitLab: Fetch new upstream contents when fork is behind (#330243) · Issues · GitLab.org / GitLab · GitLab will be implemented in iterations.

Cheers,
Michael

6 Likes