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
"refusing to merge unrelated histories" failure while pulling to recovered repository
提问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 origin
to relate my remote origin, but it alerts thatfatal: 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/***/***.git
again. It's OK. - then, I use
git push -u origin master
( I triedgit push origin master
, but I lost my local whole.git
for some reason before, so I guess it may be the first time to push, and I should add-u
). BUT, it alertserror: failed to push some refs to 'https://github.com/***/***.git'
- then, someone told me that I should use
git pull origin master
before I usepush
, 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-histories
flag 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
。
Then you for some reason lost your local .git subdirectory - which contained all your history from 1
to 2
. You managed to restore the current state though.
然后你出于某种原因失去了你的本地git的子目录-其中载有所有的历史1
来2
。不过,您设法恢复了当前状态。
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 1
from 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
1
you have cloned from as some1?
, 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 wrong1?
and need to adjust it and repeat this step - after you have found the commit
1
. You should make it your parent; dogit --reset --soft _1_
, and thengit commit
.
- 例如,根据提交时间估计
1
您从 some 克隆的提交在哪里1?
- 运行
git diff _1?_..HEAD
,并仔细阅读。确保差异仅包含您所做的编辑。如果它包含更多,那么你应该选错了1?
,需要调整它并重复此步骤 - 找到 commit 后
1
。你应该让它成为你的父母;做git --reset --soft _1_
,然后git commit
。
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 .git
directory, 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).
它可能会解决问题(我有类似的问题)。