有时候 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
网友评论