美文网首页Git
Git unrelated histories

Git unrelated histories

作者: JaedenKil | 来源:发表于2018-11-07 14:31 被阅读6次

    Sometimes when do git merge, error message will show unrelated histories, add --allow-unrelated-histories may help, but why?
    Refer to here.

    Warning: You should not use the –allow-unrelated-histories flag unless you know what is unrelated history 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.

    Pic 01

    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.

    Pic 02

    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 is your changes, so it can add them to remote project, as far as I understand it will just report massive add/add conflicts.

    What you should do now is to 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, 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_, then git commit.

    Pic 03

    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.

    相关文章

      网友评论

        本文标题:Git unrelated histories

        本文链接:https://www.haomeiwen.com/subject/lqkvxqtx.html