refusing to merge unrelated histories — How to fix this Git error
Git refuses to combine two branches that share no common ancestor. This usually happens when you initialized a repo both locally and on GitHub. Here is how to fix it.
What this error means
Every Git repository starts with an initial commit. When you merge or pull, Git looks for a common ancestor — a commit that both branches descend from. This is how Git figures out what changed on each side.
When Git says "refusing to merge unrelated histories", it means the two branches have no common ancestor at all. They are two completely independent commit trees that were never connected.
This is a safety measure added in Git 2.9. Without it, Git would silently merge two unrelated projects together, which is almost never what you want by accident. The fix is straightforward: tell Git you know what you're doing with a single flag.
Why it happens
Initializing both locally and on GitHub
You ran git init locally and made a commit. Then you created a new repo on GitHub with a README. Each has its own root commit with no shared history. When you try to git pull, Git sees two unrelated trees.
Merging two separate repositories
Sometimes you want to combine two independent projects into one. You add one repo as a remote to the other and try to merge. Since they were created independently, their histories have no common ancestor and Git refuses the merge.
git init + remote add + pull
A common beginner workflow: you run git init, add files, commit, then run git remote add origin <url> followed by git pull origin main. If the remote already has commits, they are unrelated to yours.
How to fix it
Three approaches depending on your situation: pull with the flag, merge manually, or rebase.
1. Pull with --allow-unrelated-histories
2. Merge and resolve conflicts
3. Rebase onto remote
How to prevent it
Clone first, code second
Create your repository on GitHub first (with or without a README), then git clone it to your machine. This guarantees your local and remote repos share the same initial commit, so you'll never see this error.
Push before GitHub adds files
If you prefer starting locally, create the GitHub repo without a README, license, or .gitignore. Then add the remote and push your local commits. Since the remote is empty, there are no unrelated histories to conflict with.
Use git clone for existing repos
Never run git init in a folder and then try to connect it to an existing remote repository. Use git clone <url> instead, which downloads the full history and sets up the remote automatically.
GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.
FAQ — refusing to merge unrelated histories
Master Git merges with GitQuest
Practice merging, pulling, and resolving conflicts through interactive investigations. No more guessing at flags.
Start learning Git