git push 出错的两种情况

来源:互联网 发布:java interrupt 编辑:程序博客网 时间:2024/06/05 17:19

今天改完代码调用git push的时候发生如下错误

Pushing to git@github.com:cuijianaaa/test.gitTo git@github.com:cuijianaaa/test.git ! [rejected]        master -> master (non-fast-forward)error: failed to push some refs to 'git@github.com:cuijianaaa/test.git'hint: Updates were rejected because the tip of your current branch is behindhint: its remote counterpart. Merge the remote changes (e.g. 'git pull')hint: before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.

翻译成中文意思是:由于你当前分支的末端落后与远程端对应分支,所以无法更新,请在提交之前把远端对应分支的改变合并过来再push。

可能的原因:
1、是本地做了版本的回退,比如用了git reset,所以本地分支版本落后于远程端对应分支。
2、在你push之前别人有push同一个分支的代码,导致远程比本地新。

对于1:
这种情况如果确保本地强制回退是舍掉了一些没用的东西,则可以用

git push -f

注意:
强制push本地分支将远程对应分支覆盖。这样操作一定要小心,因为push -f之后远程对应分支之前比本地的多的一些提交将丢失。如果不确保本地回退对应部分可以舍弃,则要用情况2的方法解决

对于2:

git fetch #把远程最新的更新到本地 git merge #把远程最新的合并到本地分支orgit pull #git pull = git fetch + git merge
原创粉丝点击