Refreshing a Fork

I have forked a repository that is constantly updated by the creator. How do I go about updating my forked copy so that I have the most up-to-date files?

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

Feature request in GitLab: Offer to fetch and merge new upstream contents when fork is behind (#330243) · Issues · GitLab.org / GitLab · GitLab

Cheers,
Michael

5 Likes

Thank you for that detailed instruction. With your first option (GitLab Repository Mirror), I only have the option for PUSH for Mirror Direction. Any idea why this might be?

Hi,

maybe the form needs a refresh, I’ve found it a bit wonky when adding a new item. Or the URL you’re providing above is the same as the current repository. Can you share a screenshot?

Cheers,
Michael

Hi - Please see the attached screenshot.

Look valid to me. Maybe try hitting refresh once, then copy the URL again. I had some issues with the different form states until it allowed me to change the mirror direction. Though I was using gitlab.com, not sure whether this is an advanced option only applicable in EE.

Cheers,
Michael

Seems the issue is that I’m on a hosted GitLab through my company. When I try this on gitlab.com, I have the Push/Pull option, but when I’m on my company’s site, I only have Pull.

Ah, I see. Didn’t look into the specific docs section where this feature is marked as a paid tier feature.

In that case (assuming you’re using the free tier), I’d suggest going the manual scripted way with adding that to a cronjob somewhere. Or you’ll migrate to Premium/Ultimate :slight_smile:

Cheers,
Michael

worked liked a charm :grinning: :grinning:

1 Like

Thank you, you saved my day! :)))

1 Like

A post was split to a new topic: Problem with repository mirroring for forks

I’ve linked the feature request in GitLab in the tutorial above. Offer to fetch and merge new upstream contents when fork is behind (#330243) · Issues · GitLab.org / GitLab · GitLab - please upvote and/or dive into contributing it in case you want to see it happen :slight_smile:

To: method1 : strage it only let me push this way, the select is frozen

Thanks, I thought I had added the feature availability note. I have edited my response from before joining GitLab, and added that pull is available in the Premium tier, more details in Pull from a remote repository | GitLab

Cheers,
Michael

This post was flagged by the community and is temporarily hidden.