“拒绝合并无关历史"拉到恢复的存储库时失败 [英] "refusing to merge unrelated histories" failure while pulling to recovered repository
问题描述
我想出了一个关于 Git 推送的问题.
之前成功过,这次失败了.一开始,我在我的master MINGW64/d/javasoft/apache-tomcat-7.0.70/webapps/MyNote(master)
,然后,
- 我使用
git remote add origin
来关联我的远程源,但它提醒fatal: remote origin 已经存在.
- 然后,我使用
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'
- 然后,有人告诉我应该在使用
push
之前使用git pull origin master
,我照做了.但是,它警告:fatal:拒绝合并无关的历史
. - 我在
然后由于某种原因,您丢失了本地 .git 子目录 - 其中包含您从
1
到2
的所有历史记录.不过您还是设法恢复了当前状态.但现在它没有任何历史记录 - 看起来整个项目都是凭空出现的.如果您要求 Git 合并它们,它将无法说出您的更改在哪里,因此它可以将它们添加到远程项目中,据我所知,它只会报告大量添加/添加冲突.
你应该现在从你克隆项目的远程历史中找回那个提交
1
(我假设你没有在那之后拉取;如果你这样做了,那么你应该查找您已拉取的最后一次提交).然后您应该修改您的历史记录,以便从提交 1 开始,然后 Git 将能够正确合并(例如使用 pull).因此,步骤(假设您现在处于没有历史记录的恢复提交中):
- 例如基于提交时间,估计您从某个
1
克隆的提交1
在哪里 - 运行
git diff _1?_..HEAD
,仔细阅读.确保差异仅包含您所做的编辑.如果它包含更多,那么您应该选择了有点错误的1?
,需要调整它并重复此步骤 - 在您找到提交
1
之后.你应该让它成为你的父母;做git --reset --soft _1_
,然后git commit
.
现在看起来您已经从
1
克隆,然后对所有更改进行了一次提交.无论如何,旧的.git
目录都会丢失中间历史记录,但现在您可以运行git pull
- 它会正确合并.I come up with a problem about Git push.
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,- 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
I really don't know how to do... I just want to push...
解决方案This cannot be answered shortly.
Warning: You should not use 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.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 point2
. Meanwhile, project has evolved to some point3
.Then you for some reason lost your local .git subdirectory - which contained all your history from
1
to2
. You managed to restore the current state though.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.
You should now 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).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
.
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 yourgit pull
- it will merge correctly.这篇关于“拒绝合并无关历史"拉到恢复的存储库时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 例如基于提交时间,估计您从某个