同步一个fork

GitHub 自己的帮助文档 Syncing a fork 点进去看这篇的时候,注意到有一个 Tip: Before you can sync your fork with an upstream repository, you must configure a remote that points to the upstream repository in Git.

具体步骤:

Configuring a remote for a fork

从upstream/master分支的改变合并到你的本地master分支。这使得你的fork的master分支与upstream仓库同步,不会丢失你的本地改变。

  • git克隆你fork之后的仓库
 git clone https://github.com/YOUR_USERNAME/YOUR_FORK.git
 cd YOUR_FORK
  • 给fork配置一个remote
  • 主要使用git remote -v查看远程状态
git remote -v
# origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
# origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
  • 添加一个将被同步给 fork 远程的上游仓库
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
  • 再次查看状态确认是否配置成功。
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)

Syncing a fork

使用upstream仓库同步一个fork仓库使它保持最新状态

  • 切换你当前的工作目录到你的本地仓库.

  • 从upstream仓库取得分支和它们各自的提交。提交到master分支的内容将会存储在一个本地的分支,upstream/master。
    git fetch upstream

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]      master     -> upstream/master
  • 切换到你fork的本地master分支。
    git checkout master
git checkout master
Switched to branch 'master'
  • 从upstream/master分支的改变合并到你的本地master分支。这使得你的fork的master分支与upstream仓库同步,不会丢失你的本地改变。

    git merge upstream/master

    git merge upstream/master
    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
  • 如果你的本地分支没有任何提交,Git将执行一个fast-forward:

    git merge upstream/master
    Updating 34e91da..16c56ad
    Fast-forward
    README.md                 |    5 +++--
    1 file changed, 3 insertions(+), 2 deletions(-)
  • 同步你的fork,仅更新仓库的本地副本。为了更新你的Github上的fork仓库,你必须push你的改变

git push origin master

你可能感兴趣的:(github,sync-fork)