[教學] git應用-提交落後 - 討論區

[教學] git應用-提交落後

文章瀏覽次數 954 文章回覆數 0

特種兵

特種兵圖像(預設)

2020-09-17 10:36:38

From:211.23.21.202

這是一個經常遇到的問題,也是因為有多分支與多人開發才會容易這樣。

通常我們就是跟遠端倉庫比較,因為倉庫當作是最新最穩定也是準備上正式環境的版本。

落後跟領先在語意上都很直覺了,問題是要怎麼處理,以及為什麼會這樣。

我們先來看一下落後的狀況。

$ git status                                                                    
 On branch kevinlin                                                              
 Your branch is behind 'origin/kevinlin' by 5 commits, and can be fast-forwarded.
   (use "git pull" to update your local branch)                                  

 nothing to commit, working tree clean                                           

之前我們通常只把重點放在有沒有動過的紀錄在工作區,

有的話,就看是要 add 還是繼續編輯或放棄。

現在我們仔細看一下上面的訊息,

它告訴我們:

  • 現在在 kevinlin 分支
  • 但我們比遠端倉庫的 kevinlin 落後了 5 個提交

所以,不要只看沒有檔案動過的紀錄後就開始工作了。

這樣到時就需要 merge 了。

一般狀況下,已經發現,也還沒開始工作,就先拉回來同步再說。

$ git pull origin kevinlin
省略
  * branch            kevinlin   -> FETCH_HEAD                                   
 Updating 7213a71..6946ab5                                                       
 Fast-forward                                                                    
  xxx/controllers/donorsAdminController.php     |  5 ++++-                       
  xxx/models/donorsAdminModel.php               | 19 +++++++++++++++----         
  xxx/static/js/manage_donation.js              |  9 +++++----                   
  xxx/template/admin/donors/manage_donation.tpl |  6 +++---                      
  4 files changed, 27 insertions(+), 12 deletions(-)                             
# 再看看
$ git status                                                                    
 On branch kevinlin                                                              
 Your branch is up to date with 'origin/kevinlin'.                               

 nothing to commit, working tree clean                                           

好了,沒事了,收工。那為什麼會發生這個狀況?

通常就是沒有即時更新,也就是遠端倉庫已經有人把新提焦丟上去了。

那如果是自己之前拉回來已經工作一陣子了呢?

沒關係,就照之前流程,自己都 commit 好了想推推不上去就拉回來 merge 後再推上去了。