今天改完代码调用git push的时候发生如下错误
Pushing to git@github.com:cuijianaaa/test.git
To 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 behind
hint: 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 #把远程最新的合并到本地分支
or
git pull #git pull = git fetch + git merge