上传本地项目到GitHub,git提交步骤
1. git init //初始化仓库2. git add .(文件name) //添加文件到本地仓库
3. git commit -m “first commit” //添加文件描述信息
4. git remote add origin + 远程仓库地址 //链接远程仓库,创建主分支
5. git push -u origin master //把本地仓库的文件推送到远程仓库
提交之后出现错误
git pull origin master
把本地仓库的变化连接到远程仓库主分支所以正确的步骤应该是:
1. git init //初始化仓库
2. git add .(文件name) //添加文件到本地仓库
3. git commit -m “first commit” //添加文件描述信息
4. git remote add origin + 远程仓库地址 //链接远程仓库,创建主分支
5. git pull origin master //把本地仓库的变化连接到远程仓库主分支
6. git push -u origin master //把本地仓库的文件推送到远程仓库
但是到这里可能还会报错
fatal: refusing to merge unrelated histories
如我在Github新建一个仓库,写了License然后把本地一个写了很久仓库上传。这时会发现github的仓库和本地的没有一个共同的commit所以git不让提交,认为是写错了
origin
,如果开发者确定是这个origin
就可以使用--allow-unrelated-histories
告诉git自己确定遇到无法提交的问题,一般先pull也就是使用
git pull origin master
这里的origin
就是仓库,而master
就是需要上传的分支,因为两个仓库不同,发现 git 输出refusing to merge unrelated histories
无法pull内容因为他们是两个不同的项目,要把两个不同的项目合并,git需要添加一句代码,在
git pull
之后,这句代码是在git 2.9.2版本发生的,最新的版本需要添加--allow-unrelated-histories
告诉git 允许不相关历史合并假如我们的源是origin,分支是master,那么我们需要这样写
git pull origin master --allow-unrelated-histories
如果有设置了默认上传分支就可以用下面代码git pull origin master –allow-unrelated-histories |
注意
git push -f |
git push -f
会强制将本地的修改覆盖了远程仓库的版本,将之前的commit都给冲掉了,而且无法使用通常的git reset
方式回滚,因为使用git log
查看远程仓库的提交历史已经没有在这之前提交的commit记录了。
如果想找回记录可以参考 记一次git push -f 后的回滚操作