46

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

How can I do it? I just want to push...

4
  • Have you established why the histories have diverged? You need to find out whether local and remote/origin have commits ahead of each other, and this if so, decide whether you will merge or rebase.
    – javabrett
    Commented Sep 29, 2016 at 4:02
  • "I lost my local whole .git for some reason before". So have you started new history with just your code? That could explain what you see.
    – max630
    Commented Sep 29, 2016 at 20:59
  • 1
    Basically, you definitely should not use the –allow-unrelated-histories flag in your case. Instead you should restore the history of your code, so that the last commit from the origin history you have in your code is its parent, then merge should give correct result
    – max630
    Commented Sep 29, 2016 at 22:22
  • 1
    may I ask that how can I restore the history of my code... ( finanlly I rebuilt my remote and local repo, and it was solved, but it paid. Part of my github history has gone..). I apparently do not understand history and conflict of git very well Commented Sep 30, 2016 at 2:52

2 Answers 2

133

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 the following has happened:

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.

4
  • 16
    I've found it necessary to use the flag when importing an existing repository to a newly created repository in Github.
    – H.Rabiee
    Commented Jun 13, 2017 at 16:27
  • 1
    It didn't directly solves my problem, but it was great learn more about it! Now I'm feeling more confident about resolving my issue, thanks!
    – Felix
    Commented Jan 12, 2022 at 18:08
  • 1
    i had a different issue... first fork on github. clone it. add original source as remote. pull. my fix: use fetch upstream and merge on github itself. not a single issue! whenever i tried allow-unrelated-histories before or after this fix, it always gives me conflicts which evidently shouldn't exist.
    – cregox
    Commented Mar 16, 2022 at 16:37
  • Even if you success in --allow-unrelated-histories, your git blame (i.e files histories) will no longer work, it'll always point to the other unrelated history, which is mostly useless.
    – KenIchi
    Commented Dec 19, 2023 at 10:01
-4

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

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.