GitHub - Sync Fork的專案

在Fork別人的專案後,希望持續pull原作者的內容下來,就必須做一些這個設定。

設定(Setup)


假設已經Fork完成,在專案內會發現操作以下指令:

git remote -v
# 先查看這個git有多少remote

# origin  https://github.com/user/repo.git (fetch)

# origin  https://github.com/user/repo.git (push)


git remote add upstream https://github.com/octocat/repo.git
# 新增一個叫做"upstream"的remote,後面則是你原先fork的專案git網址


git remote -v
# 這時候就會看到多了剛剛新增的remote

# origin    https://github.com/user/repo.git (fetch)

# origin    https://github.com/user/repo.git (push)

# upstream  https://github.com/octocat/repo.git (fetch)

# upstream  https://github.com/octocat/repo.git (push)

提取(Fetching)


從上述列表可看出我們專案中有兩個遠端版本,可使用git fetch remote名稱來抓取特定的遠程檔案

git fetch upstream
# 擷取 upstream 這個 remote 的 branches 下來

# 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/octocat/repo

#  * [new branch]      master     -> upstream/master

接著,可以透過git branch -va去查看總共有多少branch

git branch -va
# 顯示remote和local的branch

# * master                  a422352 My local commit

#   remotes/origin/HEAD     -> origin/master

#   remotes/origin/master   a422352 My local commit

#   remotes/upstream/master 5fdff0f Some upstream commit

合併(Merging)


現在要把 remote 的 master 合併回 local 的 master

git checkout master
# 先checkout到master主線


git merge upstream/master
# Merge upstream/master 到我們 local 上的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

注意: 如果你 local 的 master 是有修改過的,就有可能會遇到衝突,就需手動調整。

參考資料:https://help.github.com/articles/syncing-a-fork

Comments

comments powered by Disqus