fatal: refusing to merge unrelated histories
Educational use only. Content explains errors and defensive fixes for systems you own or are authorised to test. Do not use any technique here to access data, accounts, or networks without permission.
Root Cause
Git throws this fatal error when you attempt to merge two branches that do not share a common commit ancestor. This typically happens in two scenarios: 1) You created a new repository locally, made some commits, and then tried to pull from a newly created remote repository that already had an initial commit (like a README or LICENSE). 2) You are trying to combine two entirely different projects into a single repository. Git prevents this by default to avoid accidentally overwriting or merging completely unrelated codebases.
Fix / Solution
If you are absolutely certain that these two histories should be combined (e.g., merging a local project into a fresh remote repo), you can override Git's safeguard by passing the `--allow-unrelated-histories` flag to the `git pull` or `git merge` command. After doing this, you will likely need to resolve any merge conflicts before committing.
Code Snippet
# ❌ Fails due to no common ancestor
git pull origin main
# ✅ Forces the merge of unrelated histories
git pull origin main --allow-unrelated-histories