git提交方法及一些坑

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43721000/article/details/101231853

git提交方法

1.指定要提交的代码

git add .

2.提交代码并备注提交信息

git commit -m '备注信息'

3.配置要上传的远端仓库(首次上传时配置即可)

git remote add origin 远端仓库地址,github创建项目后获得

4.推送本地代码到远端仓库并指定分支,此处指定为master远端分支

git push -u origin master

此时如果没报错,则提交成功。

如果报如下错误:

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

说明远端仓库和本地仓库的代码不一致,需要进行第5步操作。

5.拉取远端仓库master分支到本地当前分支,修改代码后再次推送

git pull origin master
git push -u origin master

此时如果没有报错,则上传成功

如果在pull的时候报以下错误:

fatal: refusing to merge unrelated histories

说明出现了一个神奇的问题,需要执行第6步。

6.让 git 允许不相关历史合并,然后再次推送代码到远端

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

这样就上传成功了
————————————————
查了一下别人的总结,大致原因可查看下面的链接及内容:

原文链接:https://blog.csdn.net/lindexi_gd/article/details/52554159

如我在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 如果有设置了默认上传分支就可以用下面代码

猜你喜欢

转载自blog.csdn.net/weixin_43721000/article/details/101231853