
How to Fix Git Error 'refusing to merge unrelated histories'
It’s a classic scenario.
- You start a project locally on your laptop. You write code. You
git init. - You go to GitHub (or GitLab) and create a new repository. You check the box "Initialize with README".
- You add the remote origin locally.
- You try to
git pull origin main.
And Git slams on the brakes:
fatal: refusing to merge unrelated histories
This error sounds scary, like you've corrupted your database. It's actually a safety feature. Here is precisely how to bypass it safely.
Root Cause Analysis
By default, Git expects a merge to happen between two branches that share a common ancestor (a common "grandparent" commit). In the scenario above, your local repo started from scratch (Root A). The GitHub repo started with the README commit (Root B). Mathematically, these are two separate trees. Git doesn't know how to weave them together because they have no shared history. It assumes you might be accidentally merging two completely different projects (like merging the Linux Kernel into your Todo App).
The Solution: The --allow-unrelated-histories Flag
We need to tell Git: "I know these look different, but I promise they belong together. Smash them."
Step 1: The Command
Run your pull command again, but append this specific flag:
git pull origin main --allow-unrelated-histories
(Note: Replace main with master if your remote branch uses the old naming convention).
Step 2: Fix the Merge Conflicts
Git will now attempt the merge. Since both "trees" likely have files (your code vs. the remote README), you might get a merge conflict immediately, or it might auto-merge if filenames don't overlap.
If a vim/nano editor pops up asking for a commit message, just save and exit (:wq in vim).
Step 3: Push Your Unified History
Now that the histories are joined, your local repo contains the "Remote README" commit and your "Local Code" commits. Push it back up:
git push origin main
Alternative Fix: The "Rebase" Method
Some developers prefer a cleaner history without the "Merge branch..." commit. You can try rebasing, though it also requires the flag. But honestly? If you just started the repo, Force Pushing is an alternative, IF AND ONLY IF you don't care about the files on GitHub (like that empty README).
Destructive / Nuclear Option (Use with Caution): If you want your local code to strictly overwrite whatever is on GitHub:
git push origin main --force
Warning: This deletes the README or anything else created on the server.
Expert Tip: Prevention
[!TIP] Don't Initialize with README The best fix is prevention. When creating a new repo on GitHub for existing local code, uncheck "Initialize with README". GitHub will then give you an empty container. When you push your local code, it fills the container perfectly, and you never have to deal with unrelated histories. Add the README locally first instead.
Conclusion
The "unrelated histories" error is Git just being over-protective. The --allow-unrelated-histories flag is the standard, documented way to bridge two isolated project starts. Use it, resolve the conflict, and get back to coding.