Git refusing to merge unrelated histories on rebase

后端 未结 23 2257
旧巷少年郎
旧巷少年郎 2020-11-22 08:30

During git rebase origin/development the following error message is shown from Git:

fatal: refusing to merge unrelated histories
Error redoing m         


        
相关标签:
23条回答
  • 2020-11-22 08:39

    Since all the other answers are not actually answering the question, here is a solution inspired by this answer on a related question.

    So you get your error doing git rebase:

    $ git rebase origin/development
    fatal: refusing to merge unrelated histories
    Error redoing merge 1234deadbeef1234deadbeef
    

    This error doesn't actually cancel the rebase, but you are now in the middle of it:

    $ git status
    interactive rebase in progress; onto 4321beefdead
    Last command done (1 command done):
       pick 1234deadbeef1234deadbeef test merge commit
    

    So you can now do the merge by hand. Find out the parent commits of the original merge commit:

    $ git log -1 1234deadbeef1234deadbeef
    commit 1234deadbeef1234deadbeef
    Merge: 111111111 222222222
    Author: Hans Dampf
    Date:   Wed Jun 6 18:04:35 2018 +0200
    
        test merge commit
    

    Find out which of the two merge parents is the one that was merged into the current one (probably the second one, verify with git log 222222222), and then do the merge by hand, copying the commit message of the original merge commit:

    $ git merge --allow-unrelated 222222222 --no-commit
    Automatic merge went well; stopped before committing as requested
    $ git commit -C 1234deadbeef1234deadbeef
    [detached HEAD 909af09ec] test merge commit
     Date: Wed Jun 6 18:04:35 2018 +0200
    $ git rebase --continue
    Successfully rebased and updated refs/heads/test-branch.
    
    0 讨论(0)
  • 2020-11-22 08:40

    Firstly pull the remote changes to your local using the following command:

    git pull origin branchname --allow-unrelated-histories
    

    ** branchname is master in my case.

    When the pull command done, conflict occurs. You should solve the conflicts. I use Android Studio to solve conflicts.

    When conflicts solved, merge is done!

    Now you can safely push.

    0 讨论(0)
  • 2020-11-22 08:41

    The default behavior has changed since Git 2.9:

    "git merge" used to allow merging two branches that have no common base by default, which led to a brand new history of an existing project created and then get pulled by an unsuspecting maintainer, which allowed an unnecessary parallel history merged into the existing project. The command has been taught not to allow this by default, with an escape hatch --allow-unrelated-histories option to be used in a rare event that merges histories of two projects that started their lives independently.

    See the Git release changelog for more information.

    You can use --allow-unrelated-histories to force the merge to happen.

    0 讨论(0)
  • 2020-11-22 08:41

    WARNING THIS WILL POTENTIALLY OVERWRITE THE REMOTE REPOSITORY

    This worked for me:

    git push origin master --force
    
    0 讨论(0)
  • 2020-11-22 08:45

    I got this error when I set up a local repository first. Then I went to GitHub and created a new repository. Then I ran

    git remote add origin <repository url>
    

    When I tried to push or pull, I got the same fatal: unrelated_histories error every time.

    Here is how I fixed it:

    git pull origin master --allow-unrelated-histories
    git merge origin origin/master
    ... add and commit here...
    git push origin master
    
    0 讨论(0)
  • 2020-11-22 08:49

    I had the same problem. The problem is remote had something preventing this.

    I first created a local repository. I added a LICENSE and README.md file to my local and committed.

    Then I wanted a remote repository so I created one on GitHub. Here I made a mistake of checking "Initialize this repository with a README", which created a README.md in remote too.

    So now when I ran

    git push --set-upstream origin master
    

    I got:

    error: failed to push some refs to 'https://github.com/lokeshub/myTODs.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes
    (e.g. hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    

    Now to overcome this I did

    git pull origin master
    

    Which resulted in the below error:

    From https://github.com/lokeshub/myTODs
    branch            master     -> FETCH_HEAD
    fatal: refusing to merge unrelated histories**
    

    I tried:

    git pull origin master --allow-unrelated-histories
    

    Result:

    From https://github.com/lokeshub/myTODs
     * branch            master     -> FETCH_HEAD
    Auto-merging README.md
    CONFLICT (add/add): Merge conflict in README.md
    Automatic merge failed;
    fix conflicts and then commit the result.
    

    Solution:

    I removed the remote repository and created a new (I think only removing file README could have worked) and after that the below worked:

    git remote rm origin
    git remote add origin https://github.com/lokeshub/myTODOs.git
    git push --set-upstream origin master
    
    0 讨论(0)
提交回复
热议问题