[FIXED] Error 'fatal: refusing to merge unrelated histories' in Git
Quick Diagnostics
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 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:
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.
- Open the conflicted files in your code editor and select the content to keep.
- Stage the resolved files:
git add .
Step 3: Complete Merge Commit and Push
Finish the merge process and upload the unified history to GitHub:
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 withgit init. - If you start locally with
git init, create an empty repository on GitHub (do not select README or License options).