▚ 01 问题描述
- 🍅在使用
git push origin master
时,遇到以下错误提示:
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'http://xxxxxx.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 merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
▚ 02 问题分析
- 🍏上述错误翻译过来为:
未能将某些引用推送到http://xxxxxx.git
。
更新被拒绝,是因为远程包含您在本地没有的工作。这通常是由另一个仓库推送到同一引用引起的。您可能希望先合并远程更改(例如,提示:git pull
),然后再推送。有关详细信息,请参阅git push --help
中的“关于快进的注意事项”。
🎯这个问题往往是由 多人协作开发或者在GitHub/GitLab网页上修改仓库 引起的。
- 我们可以先使用
pull
或者fetch + merge
,把远程分支上的提交合并到本地分支之后再进行push
。 - 若确定不再需要远程分支上的提交,则可以使用
git push -f origin master
,使本地分支强制覆盖远程分支。
▚ 03 解决方案
3.1 常规方法
# 首先,将远程分支上的提交合并到本地分支
git pull origin master
# 然后,再将合并后的本地分支提交到远程分支
git push origin master
-
git push
时,会遇到下面的窗口,默认不用修改,直接使用wq
保存退出即可:
3.1暴力方法
- 将本地分支强制覆盖远程分支:
git push -f origin master