Ffile2fix
Sign in Get started

How to fix "fatal: refusing to merge unrelated histories" in Git

fatal: refusing to merge unrelated histories

Git found that the two branches you are merging have no common ancestor commit, so it refuses to combine them by default. It usually means a local repository was created with git init separately from the remote one, and both have their own first commit.

Also appears as: From github.com:user/repo * branch main -> FETCH_HEAD fatal: refusing to merge unrelated histories · error: Pulling is not possible because you have unmerged files. · hint: You have divergent branches and need to specify how to reconcile them.

Common causes

  • You ran git init locally, committed, then added a GitHub/GitLab remote that was created with a README, license or .gitignore
  • The project was uploaded to the server, git init was run there, and it is now pulling from a separate repository
  • The remote URL points to a different repository than you think
  • The remote history was rewritten from scratch (e.g. an orphan branch or a re-created repository)
  • You are merging two separate projects intentionally, such as combining repositories

How to fix it

  1. Confirm the remote is correct. Run git remote -v and git log --oneline -3 origin/main after git fetch. If the remote is the wrong repository, fix it with git remote set-url origin <url> instead of merging.
  2. Allow the merge once. If both histories really belong together, run git pull origin main --allow-unrelated-histories. Git will join them with a merge commit.
  3. Resolve conflicts. Files that exist on both sides (README.md, .gitignore, LICENSE) usually conflict. Edit them, git add the results and git commit to finish the merge.
  4. Push the combined history. Run git push -u origin main. From now on pulls work normally because the histories share a commit.
  5. Alternative: start from the remote. If your local repo has little history, clone the remote into a new folder, copy your working files in, commit and push. This avoids the merge commit entirely.

Shell

git remote -v
git fetch origin
git pull origin main --allow-unrelated-histories
# fix conflicts in README.md / .gitignore, then
git add README.md .gitignore
git commit
git push -u origin main

How to stop it happening again

  • Create the remote repository empty (no README or license) when pushing an existing project
  • Or clone the remote first and add your code to that clone
  • On servers, deploy with git clone rather than git init in an uploaded folder

Frequently asked questions

Is --allow-unrelated-histories dangerous?

It does not delete anything, but it joins two independent histories, so make sure they are the same project. Merging the wrong repository creates a messy history that must be undone with git reset.

Why did Git start refusing this?

Since Git 2.9 merges of unrelated histories are refused by default to prevent accidental merges of different projects. The flag opts in explicitly.

Can I rebase instead of merging?

Yes: git pull --rebase origin main can replay your local commits onto the remote history, avoiding the merge commit. Conflicts are resolved per commit.