Keep in sync with the source repository after Github fork

When we want to contribute our own code to an open source repository on Github, we usually first Fork the open source code repository, then go cloneto the local, submit our own changes, and finally Pull Request to merge.

Since open source repositories usually have many people contributing code together, to continue to collaborate with these project contributors, we will encounter a very common problem: how to keep in sync with the source repository?

Follow the steps below to keep in sync with Fork's source warehouse.

Configure remote warehouse

  1. First check the currently configured remote warehouse, which YOUR_USERNAMEis your Github username and YOUR_FORKFork's warehouse name.

    $ git remote -v
    > origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
    > origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
    
  2. Configure the upstream warehouse to be synchronized (that is, the source warehouse of Fork), which ORIGINAL_OWNERis the user name of the source warehouse author and the name ORIGINAL_REPOSITORYof the source warehouse.

    $ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
    
  3. Verify the configuration, the upstreambeginning is the remote warehouse

    $ git remote -v
    > origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
    > origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
    > upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
    > upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
    

Synchronize fork branch

  1. Obtain branches and their respective commits from upstream repositories. For BRANCHNAMEsubmission will be stored in the local branch upstream/BRANCHNAMEin.

    $ git fetch upstream
    > remote: Counting objects: 75, done.
    > remote: Compressing objects: 100% (53/53), done.
    > remote: Total 62 (delta 27), reused 44 (delta 9)
    > Unpacking objects: 100% (62/62), done.
    > From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
    >  * [new branch]      main     -> upstream/main
    
  2. Switch the local default branch, which is the mainbranch in this example .

    $ git checkout main
    > Switched to branch 'main'
    
  3. upstream/mainMerge changes from the upstream default branch-in this case -to the local default branch.

    $ git merge upstream/main
    > Updating a422352..5fdff0f
    > Fast-forward
    >  README                    |    9 -------
    >  README.md                 |    7 ++++++
    >  2 files changed, 7 insertions(+), 9 deletions(-)
    >  delete mode 100644 README
    >  create mode 100644 README.md
    

    This operation will synchronize the local default branch with the upstream repository without losing local changes.

    If you don't have any separate commit locally, Git will directly perform the "fast-forward" operation:

    $ git merge upstream/main
    > Updating 34e91da..16c56ad
    > Fast-forward
    >  README.md                 |    5 +++--
    >  1 file changed, 3 insertions(+), 2 deletions(-)
    

Push synchronization

The above steps only synchronize the local copy of the warehouse. To synchronize updates on GitHub, you also need to push local changes to the Github online warehouse:

git push origin main

Sometimes, pushing a local copy to an online warehouse will encounter the following error:

$ git push origin main
To https://github.com/YOUR_USERNAME/YOUR_FORK.git
 ! [rejected]        main -> main (fetch first)
error: 推送一些引用到 'https://github.com/YOUR_USERNAME/YOUR_FORK.git' 失败
提示:更新被拒绝,因为远程仓库包含您本地尚不存在的提交。这通常是因为另外
提示:一个仓库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更
提示:(如 'git pull ...')。
提示:详见 'git push --help' 中的 'Note about fast-forwards' 小节。

This means that your remote warehouse has submissions that are not included in the local copy. You only need to perform the following operations:

$ git pull origin main

Guess you like

Origin blog.csdn.net/m0_55236025/article/details/114108855