GIT-Fork代碼的更新以及提交到原分支

需求:fork 了代碼後,修改了fork後的內容,須要將改動的代碼同步到原分支。git

clone fork的代碼

$ git clone http://xxx.git  
Cloning into 'xxxxxx'...

給fork加一個remote

查看遠程倉庫fetch

$ git remote  
origin

吧遠程倉庫加入remote.net

$ git remote add upstream http://原地址.git

再次查看code

$ git remote  
origin  
upstream

這裏出現了「upstream」,說明添加遠程地址成功blog

同步fork代碼至最新

將fork的代碼和原倉庫代碼進行同步,旨在保持當前fork代碼最新
若是不一樣步直接發起merge的話,原倉庫會顯示衝突致使沒法同步rem

$ git fetch upstream  
remote: Counting objects: 7, done.  
remote: Compressing objects: 100% (6/6), done.  
remote: Total 7 (delta 4), reused 0 (delta 0)  
Unpacking objects: 100% (7/7), done.  
From http://XXXX 
 * [new branch]      DEV        -> upstream/DEV  
 * [new branch]      master     -> upstream/master

若是是使用Idea ,git fetch 的界面操做是:
項目右鍵-Git-Repository-Fetch
而後在導航欄VCS-Update Project後,右下角能夠看到upstream的分支get

切換本地分支

$ git checkout DEV  
Already on 'DEV'  
Your branch is up-to-date with 'origin/DEV'.

若是在Idea中切換能夠參考:http://www.javashuo.com/article/p-rkjfuwgt-nw.html同步

合併最新代碼

將原倉庫內容和本地分支合併,GIt命令:it

$ git merge upstream/DEV  
Updating adcbf99..6de327b  
Fast-forward  
 xxx/src/main/resources/banner.txt | 2 +-  
 1 file changed, 1 insertion(+), 1 deletion(-)

將原倉庫內容和本地分支合併,Idea操做:
右鍵-Git-Repository-Merge changes 選擇upstream分支合併到本地io

解決衝突

若是是Idea中merge的時候會直接提示,並有可視化操做界面

提交Fork

將Fork的代碼提交至遠程倉庫

同步至原倉庫

向原地址發起merge request便可

其餘操做

當時在添加remote的時候添加錯誤,致使一直fetch 不到數據
查看remote地址

$ git remote -v
origin  http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (fetch)
origin  http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (push)
upstream        http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (fetch)
upstream        http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (push)

這裏看到我吧本身的git地址添加到了upstream,致使沒法獲取原地址內容

刪除不正確的

git remote remove upstream

再次查看

$ git remote -v
origin  http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (fetch)
origin  http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (push)

刪除remote 成功, 重複以上步驟便可

相關文章
相關標籤/搜索