通常来讲开发过程当中都是先在git建立远程仓库,而后fetch到本地仓库,再进行commit push等操做,可是有时候也须要将本地已经开发的项目上传至一个空的远程仓库中,期间也是遇到很多问题,特此总结一下git
初始化仓库github
git init
将文件提交至本地仓库gitlab
git commit -m "注释"
git remote add origin <线上仓库url>
线上仓库url 为以下连接fetch
https://github.com/wenhaofan/xxx.git
url
如今已经建立好了本地仓库,并关联上了远程仓库,若是咱们直接使用git push -u origin master将本地内容推送至线上那么可能会出现出现如下错误spa
failed to push some refs to 'https://github.com/xxx/xxx.git' hint: Updates were rejected because the remote contains work that you dogit 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 pull 再push,须要先执行如下指令将远程仓库中master分支中的文件拉取到本地开发
git pull origin master
若是没有抛异常 那么就能够愉快的再次执行 git push 了,若是抛了异常,那么能够接着往下看rem
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
出现这个问题是由于本地库和远程库没有进行关联远, 而后本地推送到远程库, 远端由于这个本地库跟本身没有关联, 因此告知没法合并,该状况有两种解决方法get
第一种: 博客
先从远端库拉下来代码,而后将本地代码放入本地库中, 而后再执行push提交上去
第二种方法:
使用如下命令,把两段不相干的 分支进行强行合并
git pull origin master --allow-unrelated-histories
而后再进行提交
git push gitlab master:init