Git - git push origin master 報錯的解決方法

GitHub

有時候 git push origin master 會報錯,如下:

$ git push origin master
To github.com:smartBBer/LeetCode.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:smartBBer/LeetCode.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

錯誤原因很簡單:本地庫和GitHub中的庫不同步,解決辦法有兩種。

第一種解決方法(不要輕易使用)

加上 -f,強制推送上去,這時你的GitHub上的庫會以本地同步,

$ git push -f
Enumerating objects: 35, done.
Counting objects: 100% (35/35), done.
Delta compression using up to 4 threads
Compressing objects: 100% (24/24), done.
Writing objects: 100% (35/35), 6.35 KiB | 325.00 KiB/s, done.
Total 35 (delta 6), reused 6 (delta 1)
remote: Resolving deltas: 100% (6/6), done.
To github.com:smartBBer/LeetCode.git
 + 0794cc5...80639ea master -> master (forced update)

第二種解決方法(推薦)

使用git pull --rebase origin master合并 GitHub 和 本地 的庫,本地會多出之前不同步的文件,在使用 git push -u origin master 推送到 GitHub 庫中。

$ git pull --rebase origin master
$ git push origin master

歡迎關(guān)注我的微信公眾號

微信公眾號:柏戰(zhàn)不殆
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容