"On branch master Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to ..."解决

在git使用时,需要将本地修改的代码提交到远程库时,push时却出现以下错误

To ssh://git@localhost:*/user/project.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'ssh://git@localhost:*/user/project.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.

查看git status出现以下提示:

On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

这是为什么呢?
原因是,在你关联本地库和远程库以后,两者的文件是一致的,之后,你修改了本地库的文件,同时远程库的文件也被你修改了。当两者都有修改的时候,由push方(这里是本地库)来先做远程库和本地库的合并之后,再提交本地库的修改到远程库。因此需要在commit和push之间,做pull操作,即按以下步骤提交修改:

git add *
git commit -m 'change'
git pull
git push origin master

输出的log中没有error或hint,提交成功啦~恭喜(ps:其实当出现错误时,输出的log中都已经告诉大家解决方案啦~)

猜你喜欢

转载自blog.csdn.net/qq_35559420/article/details/78417475
今日推荐