Home » [FIXED] Error 'fatal: refusing to merge unrelated histories' in Git
Web & Code

[FIXED] Error 'fatal: refusing to merge unrelated histories' in Git

✨ Quick Answer

The error fatal: refusing to merge unrelated histories occurs when attempting to execute git pull or git merge between two Git repositories that do not share a common commit history. This frequently happens when you run git init locally while simultaneously creating a new repository on GitHub initialized with a README.md or .gitignore file.

Quick Diagnostics

Cause
Attempting to merge two branches/repositories with unrelated commit histories
Solution
Allow forced merge using --allow-unrelated-histories flag
Cause
Remote repository initialized independently with README/LICENSE files
Solution
Execute git pull origin main --allow-unrelated-histories

Quick Solution (1 Minute): Execute the pull command with the allow flag: git pull origin main --allow-unrelated-histories

🚀 Step-by-Step Troubleshooting

Step 1: Force Merge Unrelated Histories

Open your terminal in your local project folder and run:

BASH
git pull origin main --allow-unrelated-histories

(Replace main with master if your default branch uses the old naming).

This flag instructs Git to stitch together the history trees of both independent root commits.

Step 2: Resolve Any Merge Conflicts

If both the local and remote repos contain identical filenames (like README.md), Git will mark a conflict.

  1. Open the conflicted files in your code editor and select the content to keep.
  2. Stage the resolved files:
BASH
git add .

Step 3: Complete Merge Commit and Push

Finish the merge process and upload the unified history to GitHub:

BASH
git commit -m "Fix: Merge unrelated histories from remote and local"
git push origin main

Prevention Advice

Recommended security practices:

  • If you initialize a repo on GitHub with a README.md, always clone it to your machine (git clone <url>) rather than initializing locally with git init.
  • If you start locally with git init, create an empty repository on GitHub (do not select README or License options).
Author • Web Designer & App Creator

Rodolfo Castro

Web designer, app developer, and founder of SoporteCero. Specializing in UI/UX architecture, digital products, and modern web environments. Every tutorial and guide on SoporteCero is thoroughly tested and verified in our technical lab to ensure reliable, up-to-date solutions.