DEV Community

Cover image for How to sync your fork with the parent repository
Jeremy Schuurmans
Jeremy Schuurmans

Posted on

How to sync your fork with the parent repository

Right now, my fork of dev.to displays this message:

This branch is 9 commits behind thepracticaldev:master

How do I update my fork so that it's the same as thepracticaldev:master?

cd into the directory of the cloned repo

cd code/open_source/dev.to

Running git remote -v will display the current remotes

git remote -v displays my fork as the only remotes

Add the parent repository as a new remote, specifying it as the upstream repository. In this example, we will run



git remote add upstream https://github.com/thepracticaldev/dev.to.git


Enter fullscreen mode Exit fullscreen mode

Verify with git remote -v

git remote -v shows thepracticaldev/dev.to as the upstream repository

Fetch the upstream repository with git fetch upstream

Alt Text

The commits that are different from my fork are now in separate branches in my local environment. Let's merge those. git merge upstream/master

Alt Text

Now my cloned repo looks just like the upstream repository.

NOTE: any changes made locally before the merge are preserved, so you don't have to worry about losing any of your work when you do this.

From now on, whenever I need to sync with the upstream repository, all I need to do is fetch and merge.

At this point, running git push will update the forked repo and display this message:

This branch is even with thepracticaldev:master

Top comments (13)

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 🤔

Collapse
 
d2s profile image
Daniel Schildt

Another option is to create a script (for example: scripts/pull-changes-from-upstream.sh) to the repository for helping new contributors. Then people already have a helper available to keep them on track more easily. Better to have good defaults (whatever those are), and automate basics so people don't have to quess what is the development workflow of a project.

#!/usr/bin/env bash

# Add upstream Git repository
# git remote add upstream URL_OF_GIT_REPOSITORY

# Merge latest changes from the upstream Git repository
git fetch upstream
git checkout master
git merge upstream/master
Collapse
 
weptim profile image
WEPUKHULU TIMOTHY

How can I use git desktop bro

Collapse
 
jeremy profile image
Jeremy Schuurmans

Hey! I’d love to help if I can, but I’m not sure what you’re asking. Are you asking how to use git in your local environment, or how to use GitHub Desktop?

Collapse
 
weptim profile image
WEPUKHULU TIMOTHY

Github desktop man am owky with git but for the desktop just freeks me out

Thread Thread
 
jeremy profile image
Jeremy Schuurmans

I'm not familiar with GitHub Desktop since (as far as I know) it's not available for Linux, but I did see this post that might point you in the right direction:


It looks like a really good one. I hope it helps!
Collapse
 
maxdevjs profile image
maxdevjs

Hey, great article :)

I am curious: you have different commits in your master compared to upstream because not using branches for your changes or something else?

Collapse
 
jeremy profile image
Jeremy Schuurmans

any changes made locally before the merge are preserved, so you don't have to worry about losing any of your work when you do this.

Are you referencing this line?

I don’t think I had any commits in master when I put this together, but you bring up a good point. I think it would have been clearer if I had mentioned to checkout master before merging. I was just trying to say that any local changes anyone might have won’t be overwritten in the merge, but now that I think about it more, perhaps it wasn’t totally necessary for me to make that point.

Collapse
 
mmagdy profile image
Mohamed Magdy

Great article

Collapse
 
jeremy profile image
Jeremy Schuurmans

Thanks!