码迷,mamicode.com
首页 > Web开发 > 详细

使用git将本地项目上传至git仓库

时间:2019-05-09 22:01:05      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:需要   解决   oca   关联   推送   repos   执行   local   microsoft   


个人博客 地址:https://www.wenhaofan.com/article/20190508220440

介绍

一般来说开发过程中都是先在git创建远程仓库,然后fetch到本地仓库,再进行commit push等操作,但是有时候也需要将本地已经开发的项目上传至一个空的远程仓库中,期间也是遇到不少问题,特此总结一下

初始化本地仓库

初始化仓库

git init

将文件提交至本地仓库

git commit -m "注释"

关联线上仓库

git remote add origin <线上仓库url>

线上仓库url 为如下链接

https://github.com/wenhaofan/xxx.git

提交代码

    常见错误及解决方案

                现在已经创建好了本地仓库,并关联上了远程仓库,如果我们直接使用git push -u origin master将本地内容推送至线上那么可能会出现出现以下错误

        git push

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 了,如果抛了异常,那么可以接着往下看

        git pull

 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

        出现这个问题是因为本地库和远程库没有进行关联远, 然后本地推送到远程库, 远端因为这个本地库跟自己没有关联, 所以告知无法合并,该情况有两种解决方法

        第一种:

       先从远端库拉下来代码,然后将本地代码放入本地库中, 然后再执行push提交上去  

        第二种方法:

       使用以下命令,把两段不相干的 分支进行强行合并

git pull origin master --allow-unrelated-histories

        然后再进行提交

git push gitlab master:init

        

使用git将本地项目上传至git仓库

标签:需要   解决   oca   关联   推送   repos   执行   local   microsoft   

原文地址:https://www.cnblogs.com/fanwenhao/p/10841133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!