git如何同步fork的專案
阿新 • • 發佈:2018-12-14
git同步fork的專案
我們在使用git
時,經常會需要fork別人的專案,然後通過git clone
,git push
等操作來在本地進行專案的修改,這時候如果源專案發生了變化,我們又想同步這種變化,要怎麼做呢?
此時源專案中已發生變化,我們fork的專案有我們自己所做的修改。
注意,如果直接同步的話,會失去自己所做的修改,解決辦法如下。
操作步驟
首先,我們要配置遠端分支,指向源專案,這是我們同步源專案中修改的前提:
- 開啟
git bash
,檢視當前已配置好的遠端庫:
git remote -v
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git ( fetch)
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
- 新增遠端upstream 倉庫:
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)
接下來進行fork的同步:
- 首先獲取源專案中的修改,被儲存在分支
upstream/master
中:
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
- 切換到
master
分支:
git checkout master
Switched to branch 'master'
- 把分支
upstream/master
的內容merge
到本地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
完成這個操作後,本地專案就會同步源專案中的變化,而自己所做的修改不會丟失
4. 把修改上傳到github
中自己的專案,直接git push
即可,無需git add
,git commit