
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)不殆