Git报错记录:refusing to merge unrelated histories、There is no tracking information for the current branch、如何解决Git报错:更新被拒绝是因为推送的分支提示位于其远程对方后面

一、git提示:refusing to merge unrelated histories

  本地初始化了git仓库,放了一些文件进去并进行了add操作和commit提交操作;

  github创建了git仓库并建立了README文件;

  本地仓库添加了github上的git仓库作为远程仓库,起名origin;

git remote add origin https://github.com/tielemao/TielemaoMarkdown

  问题来了,本地仓库在想做同步远程仓库到本地为之后本地仓库推送到远程仓库做准备时报错了,错误如下:

fatal: refusing to merge unrelated histories
(拒绝合并不相关的历史)

解决:

  出现这个问题的最主要原因还是在于本地仓库和远程仓库实际上是独立的两个仓库。假如我之前是直接clone的方式在本地建立起远程github仓库的克隆本地仓库就不会有这问题了。
  查阅了一下资料,发现可以在pull命令后紧接着使用--allow-unrelated-history选项来解决问题(该选项可以合并两个独立启动仓库的历史)。
命令:

git pull origin master --allow-unrelated-histories

  以上是将远程仓库的文件拉取到本地仓库了。紧接着将本地仓库的提交推送到远程github仓库上即可。

二、git提示:There is no tracking information for the current branch

  这个报错的意思是:新建的本地分支在推送远程仓库时,本地的分支与远程分支没有建立关联。

1、查看关联

git branch -vv:可以查看本地分支与远程仓库的关联关系

  出现这种情况,就可以看到是没有关联

2、解决:

  按照 git 给的提示:git branch --set-upstream-to=origin/dev(分支名称),建立关联关系即可。

  建立关联之后再查看关联关系

  然后再 pull 代码就可以了。

三、如何解决Git中的问题:“更新被拒绝是因为推送的分支提示位于其远程对方后面”(Updates were rejected because a pushed branch tip is behind its remote)

  报错信息如下:

$  git push origin master
To git@git1.eu1.frbit.com:hbrosuru.git
! [rejected]        ab68c0485d -> master (non-fast-forward)
error: failed to push some refs to 'git@git1.eu1.frbit.com:hbrosuru.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and 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.

  在持续push命令时:git push origin master:dev,我尝试将提交推送到远程并获取此错误消息:

错误:无法将某些参考推送到'git@git1.eu1.frbit.com:hbrosuru.git'
提示:更新被拒绝是因为推送的分支提示是在其远程
背后:对应。检查此分支并在再次推送之前集成远程更改
提示:(例如'git pull ...')。
提示:有关详细信息,请参阅'git push --help'中的'关于快速转发的注意事项'

  这个意思是我的本地版本,比远程版本更后;因为我在本地init了git仓库,但是一直没有提交push,代码版本管理全部在本地,远程还是个空的,所以会比远程的仓库更靠前。

  解决方案最简单的那就是强制将本地推送至远程了。

// 强制将本地 master 分支推送到您的 origin 远程的 dev分支,运行
git push --force-with-lease origin master:dev

  The --force-with-lease argument will cause the force push to succeed only if your local branch is up to date with respect to the one you're pushing to. (This prevents accidental overwriting of commits you don't know about.) I strongly advise using the with-lease variant whenever possible instead of the old --force argument, which is less safe.

  --force-with-lease 只有当您的本地分支机构与您推送的分支机构保持最新状态时,才能推动成功。 (这可以防止意外地覆盖你不知道的提交。)我强烈建议使用 with-lease 变体,而不是旧的 - 强制参数,这是不安全的。

posted @ 2020-04-06 17:16  古兰精  阅读(2805)  评论(0编辑  收藏  举报