同步更新Github上的Fork项目

Introduction

目标:在github上fork了其他用户的仓库,当原作者更新仓库后,让自己fork的仓库与原仓库保持一致。

名词含义
fork仓库(YOUR_FORK):你fork到自己github账户的仓库
原始仓库(ORIGINAL_REPOSITORY): 其他用户的原始仓库

Configuring a remote for a fork

为了能fork仓库能与原始仓库同步,我们必须先为fork仓库配置新的remote(原始仓库)

  1. 打开终端,进入本地项目目录
  2. 列出fork仓库当前配置的远程仓库。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)
  1. 为fork仓库指定一个新的远程仓库upstream,即你要同步的原始仓库。
$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
  1. 再次查看状态确认是否配置成功。
$ 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

  1. 从上游仓库 fetch 分支和提交点,传送到本地,并会被存储在一个本地分支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
  1. 切换到本地主分支(如果不在的话). git checkout mster
$ git checkout master
> Switched to branch 'master'
  1. 把 upstream/master 分支合并到本地 master 上,这样就完成了同步,并且不会丢掉本地修改的内容。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
  1. 如果想更新到 GitHub 的 fork 上,直接 git push origin master 就好了。

Reference

[1] 博客-同步一个fork
[2] 官方文档-Configuring a remote for a fork
[3] 官方文档-Syncing a fork

你可能感兴趣的:(同步更新Github上的Fork项目)