DEV Community

Discussion on: How to sync your fork with the parent repository

Collapse
 
wparad profile image
Warren Parad • Edited

Wow, after getting some links here, I was sure as hell expecting to have an automated solution, like a scheduled task to automate this, or a link to a github article on how to set up fork syncing.

... Disappointed.

What's a much better thing to do is directly track upstream instead of origin.

[remote "origin"]
        url = git@github.com:wparad/forked-repository.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[remote "upstream"]
        url = git@github.com:source/original-repository.git
        fetch = +refs/heads/*:refs/remotes/upstream/*
[branch "master"]
        remote = upstream
        merge = refs/heads/master

Then just ignore your master branch fetched from origin. If you want to know how to set this up from the CLI:

git remote add upstream upstream_url
git branch --set-upstream-to upstream/master

You can even overwrite the origin master ref locally, so that it is always the same as the source:

[remote "upstream"]
        url = git@github.com:source/original-repository.git
        fetch = +refs/heads/*:refs/remotes/upstream/*
# This next line always overwrites the origin ref to the upstream ref
# This is purely optional
        fetch = +refs/heads/master:refs/remotes/origin/master

You'll need to match sure that you always git fetch --all, but that can be easily solved with a git alias.

Also if you are using Gitlab, there is already a feature to automatically support this (from three years ago: about.gitlab.com/blog/2016/12/01/h...)

Collapse
 
maxdevjs profile image
maxdevjs

wow :O I have to try this, not grasped it ... master branch fetched from origin , why I'd fetch from origin?

Collapse
 
wparad profile image
Warren Parad

By default your local branch master is set to track changes from origin. However what you really want is master tracking changes from upstream. So I'm just suggesting to ignore origin/master as a mindset shift, and then everything works out as you would expect. Sure your remote origin/master is wrong, but who cares.

Collapse
 
jeremy profile image
Jeremy Schuurmans

Thank you for sharing that! I hadn’t seen that before, and it’s always nice to learn new techniques. I can definitely see the benefit of an automated solution as well. Food for thought 🤔