當使用GitHub進行代碼push是,出現了下面的錯誤,該如何是好?
錯誤描述:
?! [rejected]??????? master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:Willido/First.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.? See the
'Note about fast-forwards' section of 'git push --help' for details.
出錯原因:
git倉庫中已經有一部分代碼,導致遠程倉庫中代碼版本與本地倉庫中的不一致,所以它不允許你直接把你的代碼覆蓋上去。
解決辦法:
1)強推
強行上傳,即利用強覆蓋方式使用本地的內容去替代git倉庫中的內容,命令如下:
?
$ git push -f
?
2)先下載再上傳
a.先將git倉庫中的內容fetch即下載到你本地中,然后再merge,命令如下:
$ git fetch
$ git merge
也可以直接pull,命令如下:
$ git pull
但此時將又會出現另外的問題,錯誤信息如下:
You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.
If you often merge with the same branch, you may want to
use something like the following in your configuration file:
??? [branch "master"]
??? remote = <nickname>
??? merge = <remote-ref>
??? [remote "<nickname>"]
??? url = <url>
??? fetch = <refspec>
See git-config(1) for details.
上面出現的 [branch "master"]是需要明確如下的內容(位于.git/config中)
[branch "master"]
? ? remote = origin
? ? merge = refs/heads/master
這是要告訴git兩件事:
1.當你處于master branch, 默認的remote就是origin。
2.當你在master branch上使用git pull時,在沒有指定remote和branch時,那么git就會采用默認的remote(即origin)來merge在master branch上所有的變更。
可以手工編輯config文件,也可以在終端上進行命令行編輯,所需命令行如下:
$ git config branch.master.remote origin
$ git config branch.master.merge refs
/heads/master
b.然后再重新pull一下,命令行如下:
$ git pull
c.最后再進行push,命令行如下:
$ git push
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
