How to Update a Repository by Setting Up a Remote

John Wachira Feb 02, 2024
How to Update a Repository by Setting Up a Remote

In this tutorial, we will discuss how to set up a central repository as the remote to our local repository so we can update our fork when changes occur in the central repository. We should always take this step before making edits to the local repository.

Update a Repository by Setting Up a Remote

Takeaway:

  1. The importance of updating a local repository before making edits.
  2. How to update a local repository from an Upstream remote (central repo)?

A fork is the individual copy of the central repository each developer adds to their GitHub account. The next step is to clone the remote repo in our account to our local machine.

We can now work on the files, commit the changes and push them to our fork in the GitHub account. The last step is to complete a Pull request to update the central repository with the changes in our fork.

Above is the typical workflow when working on joint projects with other developers.

What is left is updating our local repository with the changes other developers have pushed to the central repository. How do we go about this?

Set Up an Upstream Remote

It is important to update our local repository with the central repository before making edits to avoid merge conflicts. We need to set up the central repository as an upstream remote for our local repository.

Follow these steps.

  • Go to the central repository and tap on the Clone or Download icon to copy the repository URL.
  • Go to the directory.
    $ cd ~/Documents/GitHub/DelftStack-participants
    
  • To set up the central repository to our local repository as the upstream repo, we run:
    $ git remote add upstream https://github.com/Delftstack/Delftscopetech-participants.git
    
  • To update the local repository with the upstream, run the command below.
    $ git pull upstream master
    

Note that we have indicated the master as the recipient branch.

Example:

$ git pull upstream master
remote: Counting objects: 21, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 21 (delta 14), reused 17 (delta 10), pack-reused 0
Unpacking objects: 100% (21/21), done.
From https://github.com/Delftstack/Delftscopetech-participants.git
    74d9b7b..463e6f0  master   -> origin/master
Auto-merging _posts/institute-materials/example.md

The output above confirms that we have synced our local repository with the central repository. You can run the git status command to check the status of our local repository.

Run the git add and git commit commands to save the changes.

We can now start making edits to the files and commit the changes. To complete the cycle, we will have to push our changes to our fork in our GitHub account.

$ git push origin master

We must use this loop when working with a central remote repository.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Remote