Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

github

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 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/***/***.git again. It's OK.
  • then, I use git push -u origin master ( I tried git 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 alerts error: failed to push some refs to 'https://github.com/***/***.git'
  • then, someone told me that I should use git pull origin master before 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

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

like image 648
Calvino Shaw Avatar asked Sep 29 '16 03:09

Calvino Shaw


People also ask

How do I fix refusing to merge unrelated histories in Git?

The alternative (and longer) way of fixing the fatal: refusing to merge unrelated histories issues is to unstage your current commits, stash them, clone your required remote repository, and then place your stashed branch contents into the new clone.

What is unrelated histories in Git?

The “fatal: refusing to merge unrelated histories” Git error occurs when two unrelated projects are merged (i.e., projects that are not aware of each other's existence and have mismatching commit histories). Consider the following two cases that throw this error: You have cloned a project and, somehow, the .

How do I merge two unrelated branches?

But this doesn't mean that Git cannot perform any merge. In fact, all you need to do to merge unrelated branches is to use the flag --allow-unrelated-histories . This tells Git to combine all the files and commits of both unrelated branches into one branch, as long as there are no file conflicts.

How do you stop Git from merging?

You can use the git reset --merge command. You can also use the git merge --abort command.


1 Answers

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 point 2. Meanwhile, project has evolved to some point 3.

Enter image description here

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.

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.

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 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.

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 .git directory, but now you can run your git pull - it will merge correctly.

like image 50
max630 Avatar answered Oct 04 '22 21:10

max630