Simple steps to synchronise a Remote Fork in a Git repository

Simple steps to synchronise a Remote Fork in a Git repository

Introduction

When you collaborate in any upstream project (like an open-source project) it is likely to be asked to make a fork of the main repository. A fork is a copy of the project in your Git account. This duplicate allows you to freely experiment with changes without affecting the original project.

There is a problem when you fork a project, it is not going to have the last changes made in the original repo, so in order to avoid problems when you try to push your changes, it is better to keep your fork up to date. 

Here's how.

This method is applicable to repositories hosted in all Git platforms like GitHub, BitBucket, GitLab, Gitea or PWSLab. 

Synchronizing Your Forked Project

Once you have forked the repository, clone it in your local machine using the git clone command.

Go to the directory of the project and list the current configured remote repository for your fork.
$ git remote -v
> origin  https://example.pwslab.net/YOUR_USERNAME/YOUR_FORK.git (fetch)
> origin  https://example.pwslab.net/YOUR_USERNAME/YOUR_FORK.git (push)
Specify a new remote upstream repository that will be synced with the fork.
$ git remote add upstream https://example.pwslab.net/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
Verify the new upstream repository you’ve specified for your fork.
$ git remote -v
> origin https://example.pwslab.net/YOUR_USERNAME/YOUR_FORK.git (fetch)
> origin https://example.pwslab.net/YOUR_USERNAME/YOUR_FORK.git (push)
> upstream  https://example.pwslab.net/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
> upstream https://example.pwslab.net/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
Fetch the branches and their respective commits from the upstream repository.
$ git fetch upstream
> remote: Enumerating objects: 82, done.
> remote: Counting objects: 100% (82/82), done.
> remote: Compressing objects: 100% (44/44), done.
> remote: Total 91 (delta 53), reused 30 (delta 30), pack-reused 9
> Unpacking objects: 100% (91/91), done.
> From https://example.pwslab.net/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
>  * [new branch]  master -> upstream/master
Checkout your fork’s local master branch.
$ git checkout master
> Switched to branch 'master' 
Merge the changes from upstream/master into your local master branch.
$ git merge upstream/master
> Updating a422352..5fdff0f
The latest changes done in the original repository are now in your local project. 
To update your fork on your Git platform, you must push your changes regularly.

Conclusion

Don’t forget it’s important to keep your fork up to date in order to avoid merge conflicts or losing your work completely when you try to push your changes. These simple steps allow you to regularly sync your forked repository with the remote upstream repository. 


Have more questions? Please email us at support@peerxp.com
Also, let us know if the article is helpful!

    • Related Articles

    • How To Change Git Remote URL?

      Introduction Git remote is a pointer that refers to another copy of the repository that is usually hosted on a remote server. In some situations, like when the remote repository is migrated to another host, you need to change the remote’s URL. This ...
    • How to duplicate a Git repository? Repository mirroring simplified.

      To duplicate a repository without forking it, you can run a special clone command, then mirror-push to the new repository. Before you can duplicate a repository and push to your new copy, or mirror, of the repository, you must create the new ...
    • PWSLab Git Workshop - Useful commands and concepts

      Agenda A brief history of Git. PWSLab walkthrough. Configure your environment. Workshop. Git Introduction Visit: https://git-scm.com/about Distributed version control. It does not rely on a connection to a central server. Many copies of the complete ...
    • Mirroring PWSLab Repositories to GitHub, Bitbucket and GitLab or any Git repositories

      Introduction Repository mirroring allows for mirroring of repositories to external sources. It can be used to mirror branches, tags, and commits between repositories. A repository mirror at PWSLab will be updated automatically. You can also manually ...
    • How to apply Git Patches?

      Patch is a text file, whose contents are similar to Git diff, but along with code, it also has metadata about commits; e.g., commit ID, date, commit message, etc. We can create a patch from commits and other people can apply them to their repository. ...