上午在local本地创建一个仓库,然后在github远端创建一个仓库,然后想将本地的仓库和远端的仓库连接起来,然后问题就出现了。
按照正常的github命令,先是
git remote add origin SSH Key
,
例如git remote add origin git@github.com:michaelliao/learngit.git
,
这一步没有问题,然后
git push -u origin master
,就出现错误了。如下所示:
$ git push -u origin master
To git@github.com:******/Demo.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to ...
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.
从它的hint可以看出,Updates were rejected because the tip of your current branch is behind,当前分支没有和远程仓库同步,所以就找解决办法,然后有人说要
git pull --rebase origin master
,
这个指令没有错误,然后以为解决了,继续
git push -u origin master
又错了,但是错误原因不一样,具体的内容是说有conflict没有解决掉,然后解决方法之一是
git pull
,但是意料之中报错了,
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>'
as appropriate to mark resolution, or use 'git commit -a'
中间记不太清了,最后得到了下面这样子的一个错误:
error : you need to resolve your current index first
继续找解决办法,最后终于在@wenwenxiong的帮助下,找到了解决办法,大致原因是merge导致的:
解决半法就是直接退回merge前,然后再push,执行以下代码:
git reset --merge
执行完这句话以后,后面的指令可以全部实现了,可以成功push到远程仓库了。
上面查找解决办法的过程中,有另外一种方法,但我没有试,写出来如下:
git push -u origin master -f
也就是强行push,但是这样子多人一起开发的时候,就会出现别人的提交会被覆盖,一般不建议这么做。