Fix git "fatal: refusing to merge unrelated histories"
git pull fails with refusing to merge unrelated histories when no commit is shared. Check the remote, then merge with --allow-unrelated-histories or rebase.
fatal: refusing to merge unrelated historiesGit refused the merge because your local branch and the remote branch have no common ancestor commit. If the remote is the right one, merge once with git pull --no-rebase origin main --allow-unrelated-histories (the --no-rebase states that you want a merge — without it, git 2.43 in its default state first stops asking you to choose merge or rebase, and anyone with pull.rebase true gets a rebase), or replay your commits on top with git rebase origin/main. The usual trigger: a project started locally with git init, pushed for the first time to a GitHub repository that was created with a README or LICENSE.
[1] Recognize the sequence that leads here. The first push is rejected because the remote has commits you don't have.
$ git push -u origin mainTo /srv/remote.git ! [rejected] main -> main (fetch first)error: failed to push some refs to '/srv/remote.git'hint: Updates were rejected because the remote contains work that you do nothint: have locally. This is usually caused by another repository pushing tohint: the same ref. If you want to integrate the remote changes, usehint: 'git pull' before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.If the histories do share an ancestor and you only got this rejection, this is not your case — see git push rejected non-fast-forward.
Following the hint, git pull on git 2.43 first stops with fatal: Need to specify how to reconcile divergent branches. — you haven't chosen merge or rebase yet. Choose merge (pull.rebase false) and the error from this page appears.
$ git config pull.rebase false$ git pull origin mainFrom /srv/remote * branch main -> FETCH_HEADfatal: refusing to merge unrelated histories[2] Check the remote URL first. Pointing origin at the wrong repository gives exactly the same error, and allowing the merge then mixes someone else's project into yours.
$ git remote -vorigin /srv/remote.git (fetch)origin /srv/remote.git (push)$ git fetch origin$ git merge-base HEAD origin/main; echo "exit=$?"exit=1$ git rev-list --max-parents=0 --all76063282d9ad16fc22c28a69da70e96d4890df20470cdfa6aa74c882280d1a55a0de44001fe721a2git merge-base printing nothing with exit code 1 means the two targets (HEAD and origin/main) have no common ancestor — that is the test to rely on. The root-commit list below it (commits with no parent) uses --all, so it counts every branch and only illustrates how this test repository is shaped; a repository that already merged an unrelated history once, or one with a separate root on another branch, also shows two or more. If the URL is not the repository you meant, fix it with git remote set-url origin <correct-url> and stop here.
[3] List what the remote has. A merge brings these files into your tree.
$ git ls-tree --name-only origin/mainLICENSEREADME.mdIf it is only the README and LICENSE added when the repository was created, pick [4] or [5]. GitHub's own guide says to create the repository without README, license, or gitignore files when you are about to push existing local code, to avoid errors.
[4] Join the histories with a merge. --allow-unrelated-histories skips the common-ancestor check for this one merge. Files with the same name on both sides conflict. The repository below has pull.rebase false from [1], so the command runs without --no-rebase; add it if you don't have that setting.
$ git pull origin main --allow-unrelated-historiesFrom /srv/remote * branch main -> FETCH_HEADAuto-merging README.mdCONFLICT (add/add): Merge conflict in README.mdAutomatic merge failed; fix conflicts and then commit the result.$ git status --shortA LICENSEAA README.mdAA means both sides added the file. Edit it by hand, or keep your local version with --ours.
$ git checkout --ours README.mdUpdated 1 path from the index$ git add README.md$ git commit --no-edit[main c9327f6] Merge branch 'main' of /srv/remote$ git log --oneline --graph* c9327f6 Merge branch 'main' of /srv/remote|\ | * 470cdfa Initial commit* 7606328 first local commit$ git push -u origin mainTo /srv/remote.git 470cdfa..c9327f6 main -> mainbranch 'main' set up to track 'origin/main'.History keeps a merge commit with two roots. To back out in the middle of the conflict, git merge --abort returns to the state before the merge. Below, the same conflict was produced in a separate test repository and then aborted.
$ git merge --abort$ git log --oneline7c829a5 first local commit[5] Rebase instead if you want a single line of history. Rebase did not need --allow-unrelated-histories. Your local commits move on top of the remote's first commit and get new hashes.
$ git rebase origin/mainSuccessfully rebased and updated refs/heads/main.$ git log --oneline --graph* c16ee8b first local commit* 470cdfa Initial commit[6] Don't look for a config switch. The git merge documentation says no configuration variable to enable this by default exists or will be added — pass the option each time. The check arrived in git 2.9.
Official sources: git-merge --allow-unrelated-histories · git 2.9 release notes · GitHub — Adding locally hosted code to GitHub