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

介绍
一般来说开发过程中都是先在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值