同步一个 fork

平时都是用 Hg(Mercurial),Git 用得不是很熟练,老是遗忘,特此记录一下。
其实都是根据 GitHub 官方 help 整理的(Syncing a forkConfiguring a remote for a fork)。

clone

配置 remote

  1. 使用git remote -v查看远程状态

    1
    2
    3
    $ git remote -v
    origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
    origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
  2. 添加一个上游仓库

    1
    $ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
  3. 再次确认

    1
    2
    3
    4
    5
    $ 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)

同步

  1. 从上游仓库 fetch 分支和提交点,传送到本地,并会被存储在一个本地分支 upstream/master

    1
    2
    3
    4
    5
    6
    7
    $ 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
  2. 切换到本地主分支(如果不在的话)

    1
    2
    $ git checkout master
    Switched to branch 'master'
  3. 把 upstream/master 分支合并到本地 master 上,这样就完成了同步,并且不会丢掉本地修改的内容。

    1
    2
    3
    4
    5
    6
    7
    8
    $ 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
  4. 最后直接git push origin master,大功告成。