Tag Archives: fatal: refusing to merge unrelated histories

fatal: refusing to merge unrelated histories [How to Solve]

Today, a new project on git was pulled locally and git pull origin development was found to remind fatal: refusing to merge unrelated histories

Query result: the reason is that the two branches have different versions and different submission history

Solution: (– allow-unrelated-histories)

git pull origin develop –allow-unrelated-histories

You can allow irrelevant history and forced merger, which has indeed solved this problem.

Git | fatal: refusing to merge unrelated histories [How to Solve]

Scenario: create an empty warehouse in gitee and push the existing warehouse. Through remote add origin < git path> specified warehouse, error message: error: failed to push some refs to ' https://gitee.com/XXXX/XXXXX.git ' Terminal Information:

JerrydeMacBook-Pro:web-h5 jerrylam$ git push      
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

After executing git push --set-upstream origin master, it reports another error, as follows .

JerrydeMacBook-Pro:web-h5 jerrylam$ git push --set-upstream origin master
To https://gitee.com/XXXX/web.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/XXXX/web.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Try git pull origin master , the error message is as follows

JerrydeMacBook-Pro:web-h5 jerrylam$ git pull origin master
From https://gitee.com/XXXX/web.git
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

Solutions

# Change the previous git pull origin master to the following code

git pull origin master --allow-unrelated-histories

Finally, we can push it

JerrydeMacBook-Pro:web-h5 jerrylam$ git push origin master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 3.77 KiB | 3.77 MiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Powered by Gitee.com
To https://gitee.com/XXXX/web.git
   f6b4cf0..c538ad9  master -> master

When push doesn’t go up, first git pull ensures the latest code

git pull  --allow-unrelated-histories
# Or specify branches
git pull origin master --allow-unrelated-histories