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.
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.
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:
upstream
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.
To fork the repository, you need to click the fork button (the upper right corner of the screenshot).
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:
Open the terminal and run the following command:
git clone git@github.com:sdaityari/e-cidadania.git
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
To verify that the remote repository has been added, check the remote repository list by running the following command:
git remote -v
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>
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":
To pull these changes to your local repository, run the following command:
git pull upstream master
This command will update your master branch from the upstream
remote repository.
origin
(forked repository) To push these updates from the original repository to the forked repository, just run the following command:
git push origin master
To confirm that the changes have been updated, visit the forked repository page on GitHub again!
Message "This branch is consistent with cidadania:master" indicates that the main branch that has been added to the forked repository is changed.
(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!