之前自己做的项目提交git时经常会出现这个问题,为了加深自己印象,专门记录下解决方案。
git 报错:
To https://github.com/hellojinsilei/community.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/hellojinsilei/community.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决方案:
报错原因其实是我们本地未与远端同步,我之前使用了git remote add origin xxx(远端仓库地址)这仅仅是本地与远端建立了连接,并不是同步了,如果我是直接从远端clone下来的就可以直接用git push -u origin master就不会有问题,但是现在报错也是很明显的,我们并没有同步,所以我们需要把远端和本地同步,具体的命令就是
git pull --rebase origin master
之后再进行git push origin master 就可以成功了!