1. 程式人生 > 實用技巧 >Git錯誤,Updates were rejected because the tip of your current branch is behind

Git錯誤,Updates were rejected because the tip of your current branch is behind

問題:Updates were rejected because the tip of your current branch is behind

在push程式碼時,遇到這種問題Updates were rejected because the tip of your current branch is behind
(更新被拒絕,因為當前分支的尖端落後)

解決

有三種方案

  1. push前先將遠端repository修改pull下來
git pull origin master
git push -u origin master
  1. 使用強制push的方法:
git push -u origin master -f 

這樣會使遠端修改丟失,一般是不可取的,尤其是多人協作開發的時候。

  1. 若不想merge遠端和本地修改,可以先建立新的分支:
git branch [name]
然後push
git push -u origin [name]

衍生問題

使用上述方法在pull時可能會遇到如下問題

  1. git pull 提示refusing to merge unrelated histories
    這個有解決辦法

  2. There is no tracking information for the current branch(沒有當前分支的跟蹤資訊)
    是因為本地分支和遠端分支沒有建立聯絡
    (使用git branch -vv

    可以檢視本地分支和遠端分支的關聯關係,檢視遠端分支 git remote -v)
    解決方法:
    1)是直接指定遠端master:

git pull origin master

​2)另外一種方法就是先指定本地master到遠端的master,然後再去pull:

git branch --set-upstream-to=origin/遠端分支的名字  本地分支的名字
git pull

關於git remote可以看看這篇文章