Skip to content
FR
  1. LuzineDebug
  2. Git & GitHub
  3. fatal: refusing to merge unrelated histories
Git & GitHub Beginner ≈ 5 min to fix

Fix "fatal: refusing to merge unrelated histories" in Git

Error message
fatal: refusing to merge unrelated histories
  • git pull origin main -> fatal: refusing to merge unrelated histories
  • There isn't anything to compare. main and feature are entirely different commit histories.
  • fatal: refusing to merge unrelated histories (git merge origin/master)
In short

The two branches you are trying to merge share no commit, usually because the GitHub repository was created with a README and the local one with git init. If that is intended, merge with git pull origin main --allow-unrelated-histories, then resolve the conflicts.

Be the first to confirm this fix

What this error means

A Git merge relies on a common ancestor (merge base) to know what changed on each side. When the two histories start from different root commits, there is no common ancestor. Since Git 2.9, git merge and git pull refuse this kind of merge by default, because it is often a sign of a mistake: wrong remote, recreated .git folder, or wrong branch. The --allow-unrelated-histories option lifts that safeguard when you know that joining the two histories is really what you want.

Common causes

  1. Remote repository initialised with a README, licence or .gitignore on GitHub/GitLab, then a local project created separately with git init.
  2. Wrong remote: origin points to a different project than you think.
  3. .git folder deleted and recreated: the new local history starts from scratch and no longer shares anything with the remote.
  4. Intentional merge of two projects (importing a repository into a monorepo): the option is legitimate here.

Step-by-step fix

  1. Check the remote

    Make sure origin is the right repository and look at its history. If the URL is wrong, fix it instead of forcing the merge.

    bash
    git remote -v
    git fetch origin
    git log --oneline origin/main -n 10
    # wrong URL?
    git remote set-url origin [email protected]:user/repo.git
  2. Merge allowing unrelated histories

    If both histories really need to be joined, run the pull again with the option. Git creates a merge commit that links the two roots.

    bash
    git pull origin main --allow-unrelated-histories
  3. Resolve conflicts

    Files present on both sides (often README.md, .gitignore, LICENSE) conflict as "both added". Keep the content you want, then finish the merge.

    bash
    git status
    # edit README.md / .gitignore, then:
    git add README.md .gitignore
    git commit
  4. Push and track the branch

    Push the result and link your local branch to the remote one for future git pull and git push.

    bash
    git push -u origin main
  5. Alternative: the remote only has a useless README

    If the remote repository was just created and you do not care about its auto-generated README, you can replace it with your history using --force-with-lease. This permanently removes the remote commits: never do it on a repository where others have already worked.

    bash
    git push --force-with-lease origin main

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • To push an existing local project, create the GitHub repository empty (no README, licence or .gitignore).
  • If the remote repository already exists, start with git clone and copy your files into it, instead of git init.
  • Never delete the .git folder to "start clean": use git reset or a new branch.

Frequently asked questions

Is --allow-unrelated-histories dangerous?

It deletes nothing: it only allows a merge Git refuses by default. The risk is joining two unrelated projects if origin is the wrong repository, which is why you should check git remote -v first.

How do I do it in GitHub Desktop or VS Code?

These tools do not expose the option. Open the integrated terminal in the project folder and run git pull origin main --allow-unrelated-histories, then go back to the UI to resolve the conflicts.

Why does GitHub say "entirely different commit histories" on my pull request?

The two branches of the PR share no ancestor. Merge locally first with --allow-unrelated-histories, push the branch, and the comparison works again.

Useful tools for this error

More Git & GitHub errors

All Git & GitHub errors →