需求:本地有git的代码仓库,远程也有代码仓库(没有则必须新建一个),两个是独立的,现在要将它们关联起来。
1、本地执行 git remote add origin https://github.com/henjing/test.git (你自己的仓库地址),origin 是远程库名字,这是默认的,你也可以自己改。
2、执行 git remote -v 查看,如下图,就说明关联了远程库:

3、git push -u origin master 第一次提交需要加 -u 以后就不用了。执行完命令你可能会看到如下失败的提示:

大概意思是你需要先执行 git pull 把远程库的改变拿下来才能提交代码上去。接着当然就是执行 git pull origin master 把代码pull下来,但是你可能又一次失败:

合并被拒绝了。这时你需要执行这个命令:git pull origin master --allow-unrelated-histories 才可以pull下来(看了好多都没有说到这步)。现在你可以push代码到远程仓库了,收工!!!
网友评论