git 拉到恢复的存储库时“拒绝合并不相关的历史”失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39761024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:32:13  来源:igfitidea点击:

"refusing to merge unrelated histories" failure while pulling to recovered repository

gitgithub

提问by Calvino Shaw

I come up with a problem about Git push.

我想出了一个关于 Git 推送的问题。

It worked successfully before, but it failed this time. In the beginning, I am in my master MINGW64 /d/javasoft/apache-tomcat-7.0.70/webapps/MyNote (master), and then,

之前成功过,这次失败了。一开始,我在我的主人MINGW64 /d/javasoft/apache-tomcat-7.0.70/webapps/MyNote (master),然后,

  • I use git remote add originto relate my remote origin, but it alerts that fatal: remote origin already exists.
  • then, I use git remote rm origin(Someone told me to), and it's OK.
  • then, I use git remote add origin https://github.com/***/***.gitagain. It's OK.
  • then, I use git push -u origin master( I tried git push origin master, but I lost my local whole .gitfor some reason before, so I guess it may be the first time to push, and I should add -u). BUT, it alerts error: failed to push some refs to 'https://github.com/***/***.git'
  • then, someone told me that I should use git pull origin masterbefore I use push, and I did as it. BUT, it alerted: fatal: refusing to merge unrelated histories.
  • I found some answers in Git refusing to merge unrelated histories on rebase, but it seemed that it didn't work. In my issue, it alerted fatal: Couldn't find remote ref –allow-unrelated-histories
  • git remote add origin过去常常将我的远程来源联系起来,但它警告说fatal: remote origin already exists.
  • 然后,我使用git remote rm origin(有人告诉我使用),就可以了。
  • 然后,我git remote add origin https://github.com/***/***.git再次使用。没关系。
  • 然后,我使用git push -u origin master(我试过git push origin master,但我.git之前由于某种原因丢失了我的本地整体,所以我想这可能是第一次推送,我应该添加-u)。但是,它提醒error: failed to push some refs to 'https://github.com/***/***.git'
  • 然后,有人告诉我应该在使用git pull origin master之前使用push,我就这样做了。但是,它提醒:fatal: refusing to merge unrelated histories
  • 我在Git 拒绝合并 rebase 上不相关的历史中找到了一些答案,但它似乎不起作用。在我的问题中,它提醒了fatal: Couldn't find remote ref –allow-unrelated-histories


I really don't know how to do... I just want to push...

真不知道怎么办了……就是想推……

回答by max630

This cannot be answered shortly.

这不能一蹴而就。

Warning: You should notuse the --allow-unrelated-historiesflag unless you know what unrelated history is and are sure you need it. The check was introduced just to prevent disasters when people merge unrelated projects by mistake.

警告:除非您知道不相关的历史是什么并且确定需要它,否则您不应使用该--allow-unrelated-histories标志。引入检查只是为了防止人们错误地合并不相关的项目时发生灾难。

As far as I understand, in your case has happened the following:

据我了解,在您的情况下发生了以下情况

You have cloned a project at some point 1, and made some development to point 2. Meanwhile, project has evolved to some point 3.

您在某个时候克隆了一个项目1,并针对这一点进行了一些开发2。同时,项目已经发展到一定程度3

Enter image description here

在此处输入图片说明

Then you for some reason lost your local .git subdirectory - which contained all your history from 1to 2. You managed to restore the current state though.

然后你出于某种原因失去了你的本地git的子目录-其中载有所有的历史12。不过,您设法恢复了当前状态。

Enter image description here

在此处输入图片说明

But now it does not have any history - it looks like the whole project has appeared out of nowhere. If you ask Git to merge them it will not be able to say where your changes are, so it can add them to remote project, as far as I understand it will just report massive add/add conflicts.

但是现在它没有任何历史记录 - 看起来整个项目都是凭空出现的。如果您要求 Git 合并它们,它将无法说出您的更改在哪里,因此它可以将它们添加到远程项目中,据我所知,它只会报告大量的添加/添加冲突。

You shouldnow find back that commit 1from the remote history where you have cloned the project (I assume you did not pull after that; if you did then you should instead look for the last commit you have pulled). Then you should modify your history so that is starts from that commit 1, and then Git will be able to merge correctly (with pull for example).

现在应该1从克隆项目的远程历史记录中找到该提交(我假设您在那之后没有拉取;如果您这样做了,那么您应该查找您拉取的最后一次提交)。然后您应该修改您的历史记录,以便从提交 1 开始,然后 Git 将能够正确合并(例如使用 pull)。

So, the steps (assuming you are now in your restored commit without history):

因此,步骤(假设您现在处于没有历史记录的恢复提交中):

  • estimate where is the commit 1you have cloned from as some 1?, based on commit time for example
  • run git diff _1?_..HEAD, and read carefully. Make sure that the difference contains only edits which you have made. If it contains more then you should have picked a bit wrong 1?and need to adjust it and repeat this step
  • after you have found the commit 1. You should make it your parent; do git --reset --soft _1_, and then git commit.
  • 例如,根据提交时间估计1您从 some 克隆的提交在哪里1?
  • 运行git diff _1?_..HEAD,并仔细阅读。确保差异仅包含您所做的编辑。如果它包含更多,那么你应该选错了1?,需要调整它并重复此步骤
  • 找到 commit 后1。你应该让它成为你的父母;做git --reset --soft _1_,然后git commit

Enter image description here

在此处输入图片说明

Now it looks like you have cloned from 1, then made one commit with all your changes. Your intermediate history is lost anyway with your older .gitdirectory, but now you can run your git pull- it will merge correctly.

现在看起来您已经从 克隆1,然后对所有更改进行了一次提交。无论如何,您的旧.git目录都会丢失中间历史记录,但是现在您可以运行您的git pull- 它会正确合并。

回答by Wheel60

Try to commit the changes to the local repository efore pushing them onto GitHub.

在将更改推送到 GitHub 之前,尝试将更改提交到本地存储库。

It may solve the issue (I had a similar issue).

它可能会解决问题(我有类似的问题)。