Home > Technology peripherals > It Industry > Quick Tip: Sync a GitHub Fork via the Command Line

Quick Tip: Sync a GitHub Fork via the Command Line

Christopher Nolan
Release: 2025-02-19 09:41:12
Original
534 people have browsed it

It is crucial to keep your GitHub forked repository in sync and make sure your forked repository is consistent with the latest changes from the original repository. This can be done by pulling changes from the original repository to the local repository and pushing it to the forked repository.

The process of updating a forked repository includes: forking the repository, cloning the forked repository, linking to the original repository, pulling changes from the original repository, and pushing changes to the forked repository. This process assumes that you have forked the repository and cloned the forked repository on your local machine.

When dealing with forked repositories, it is best to avoid committing changes directly to the main branch of the forked repository or the local repository. This branch should be used to save update code from the original repository only. All changes should be made in a new feature or error branch and pushed to a branch with the same name on the forked repository.

Quick Tip: Sync a GitHub Fork via the Command Line

To understand the concept of updating a forked repository, you must first understand why this is necessary.

Organizations cannot grant write permissions to their primary repository to each potential contributor, so the public can only view the original repository. A fork is a copy of the original repository that a user can create. The user has read and write permissions for their own forks.

Usually, programming is done on a local machine (or virtual machine) rather than directly on the GitHub interface, so a clone of the forked repository is usually created.

Once the contributor submits a change to the local replica, it needs to be pushed to a forked repository on GitHub (this is possible due to write permissions). Then, create a pull request from the forked repository to the original repository.

Keep your forked repository up to date

When the original repository is updated with someone else's code (after the fork is created), these new commits will not automatically appear in the fork repository. These changes must first be downloaded and merged to the local repository and then pushed to the forked repository.

For historical reasons, in our local repository, we named the remote repository of the original repository upstream and the forked repository origin.

Ideally, you should never make any commits directly to the main branch of a forked repository or local repository. This branch must be used only to save update code from upstream. All changes must be made in a new feature or error branch and pushed to a branch with the same name on the forked repository.

Therefore, the following steps help to update the forking repository with the latest commit from the original repository:

  • Pull the main branch of the local repository from the main branch of upstream
  • Push from the main branch of the local repository to the main branch of the forked repository

These steps assume that you have forked the repository and cloned the forked repository on your local machine.

For demonstration, we will use the repository of e-Cidadania on GitHub.

Step 1: Fork the warehouse

To fork the repository, you need to click the fork button (the upper right corner of the screenshot).

Quick Tip: Sync a GitHub Fork via the Command Line

Step 2: Clone your forked repository

To clone your forked repository, you first need to select the protocol from the drop-down menu (as shown in the screenshot below) and copy the link. In this demonstration, we will select the SSH protocol:

Quick Tip: Sync a GitHub Fork via the Command Line

Open the terminal and run the following command:

git clone git@github.com:sdaityari/e-cidadania.git
Copy after login

Step 3: Link to the original repository

You then need to link your local repository to the original repository to be able to pull changes from the original repository. This is done by adding a upstream remote repository. First, copy the SSH link from the original repository and add the remote repository by running the following command:

git remote add upstream git@github.com:cidadania/e-cidadania.git
Copy after login

To verify that the remote repository has been added, check the remote repository list by running the following command:

git remote -v
Copy after login

The output should look like this:

<code>origin  git@github.com:sdaityari/e-cidadania.git (fetch)
origin  git@github.com:sdaityari/e-cidadania.git (push)
upstream    git@github.com:cidadania/e-cidadania.git (fetch)
upstream    git@github.com:cidadania/e-cidadania.git (push)</code>
Copy after login

Step 4: Pull changes from upstream (original repository)

When there are new commits in the main branch of the original repository and there are no commits in your fork repository, you will receive a GitHub message on the fork repository's page. In the screenshot, you can see a message that says "This branch lags behind cidadania:master 36 commits":

Quick Tip: Sync a GitHub Fork via the Command Line

To pull these changes to your local repository, run the following command:

git pull upstream master
Copy after login

This command will update your master branch from the upstream remote repository.

Step 5: Push changes to origin (forked repository)

To push these updates from the original repository to the forked repository, just run the following command:

git push origin master
Copy after login

To confirm that the changes have been updated, visit the forked repository page on GitHub again!

Quick Tip: Sync a GitHub Fork via the Command Line

Message "This branch is consistent with cidadania:master" indicates that the main branch that has been added to the forked repository is changed.

FAQs (FAQs) on syncing GitHub fork repositories via command line

(The FAQ part is omitted here because it is too long and does not match the pseudo-original goal. The FAQ part can be reorganized and rewritten as needed, but the information must be ensured that the information is complete and the original intention is not changed.)

The above is the detailed content of Quick Tip: Sync a GitHub Fork via the Command Line. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template