将本地代码提交到远程Git上

第一步:建立git仓库 
cd到你的本地项目根目录下,执行git命令

git init

 

第二步:将项目的所有文件添加到仓库中

git add .

 

如果想添加某个特定的文件,只需把.换成特定的文件名即可

第三步:将add的文件commit到仓库

git commit -m "注释语句"

第四部:创建远程分支

第五步:重点来了,将本地的仓库关联到github上

$ git remote add origin git@github.com:ZhuBaker/learngit.git

第六步:上传github之前,要先pull一下,执行如下命令:

git pull origin master

敲回车后,会执行输出类似如下 

这里写图片描述

第六步可能出现如下错误:

$ git pull origin master
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:ZhuBaker/learngit
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
fatal: refusing to merge unrelated histories

本文讲的是把git在最新2.9.2,合并pull两个不同的项目,出现的问题如何去解决

fatal: refusing to merge unrelated histories
我在Github新建一个仓库,写了License,然后把本地一个写了很久仓库上传。
先pull,因为两个仓库不同,发现refusing to merge unrelated histories,无法pull
因为他们是两个不同的项目,要把两个不同的项目合并,git需要添加一句代码,在git pull,这句代码是在git 2.9.2版本发生的,最新的版本需要添加--allow-unrelated-histories

假如我们的源是origin,分支是master,那么我们 需要这样写

git pull origin master ----allow-unrelated-histories


需要知道,我们的源可以是本地的路径

$ git pull origin master --allow-unrelated-histories
From github.com:ZhuBaker/learngit
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 abc.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 abc.txt

 

 

第七步,也就是最后一步,上传代码到github远程仓库

git push -u origin master

执行完后,如果没有异常,等待执行完就上传成功了,中间可能会让你输入Username和Password,你只要输入github的账号和密码就行了

 

 

第七部可能出现如下错误信息:

$ git push -u origin master
To git@github.com:******/Demo.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:******/Demo.git'
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.

 

转载于:https://my.oschina.net/LucasZhu/blog/1550506

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值