Fix 'fatal: refusing to merge unrelated histories' in Git

beginner๐Ÿ“ฆ Git2026-03-18| Git 2.9+ on Linux, macOS, Windows โ€” occurs during git merge, git pull, or git rebase

Error Message

fatal: refusing to merge unrelated histories
#git#merge#history#unrelated

The Error

You run git merge or git pull and get hit with:

fatal: refusing to merge unrelated histories

The two branches share no common commit ancestor โ€” their histories are completely separate lineages. Git 2.9 added this safety check specifically to stop you from accidentally smashing two unrelated projects together.

When Does This Happen?

  • You ran git init locally and later added a remote that already had commits (e.g., a GitHub repo initialized with a README)
  • You're merging two separate repositories together intentionally
  • The .git history was wiped and re-initialized on one side
  • You cloned a shallow repository and are trying to merge with a full one

Fix 1: Allow Unrelated Histories (Most Common)

Pass the --allow-unrelated-histories flag. That's it. It tells Git you're aware of the situation and want to proceed:

# If the error occurred during git merge
git merge origin/main --allow-unrelated-histories

# If the error occurred during git pull
git pull origin main --allow-unrelated-histories

Git will open your configured editor for a merge commit message. Save and close it to finish.

Merging a local branch instead of a remote? Same flag:

git merge feature-branch --allow-unrelated-histories

Fix 2: Rebase with Unrelated Histories

Prefer a linear history without a merge commit? Use rebase:

git rebase origin/main --allow-unrelated-histories

One caveat: rebase rewrites commit hashes. Don't use this on shared branches that teammates have already pulled from โ€” you'll cause headaches for everyone.

Fix 3: The "New Local Repo + Existing Remote" Scenario

Nine times out of ten, this is the situation: you ran git init, made a few commits, then tried to pull from a GitHub repo that was created with a README or LICENSE already in it.

# Step 1: Add the remote (if you haven't already)
git remote add origin https://github.com/username/repo.git

# Step 2: Fetch the remote refs
git fetch origin

# Step 3: Merge allowing unrelated histories
git merge origin/main --allow-unrelated-histories

# Step 4: Push the combined history
git push -u origin main

Fix 4: Merging Two Separate Repos Together

Consolidating two repos into one โ€” monorepo-style? Add the second repo as a remote, pull it in, then clean up:

# In your target repo, add the other repo as a remote
git remote add other-repo https://github.com/username/other-repo.git
git fetch other-repo

# Merge it in
git merge other-repo/main --allow-unrelated-histories

# Clean up the remote reference
git remote remove other-repo

Move the imported files into a subdirectory right away to keep things organized:

mkdir imported-project
git mv file1.txt file2.txt imported-project/
git commit -m "Move imported files into subdirectory"

Verify the Fix Worked

Run these two commands to confirm everything landed correctly:

# Check the merge commit appears in log
git log --oneline --graph --all

# Verify the working tree is clean
git status

The log should show a merge commit at the top connecting both histories. Running git status should return a clean working tree โ€” or just your expected uncommitted changes.

Push to confirm there are no surprises upstream:

git push origin main

Prevention

The root cause is always the same: both sides started with their own independent commits. Eliminate that, and you'll never hit this error.

  • Create the remote repo empty โ€” on GitHub or GitLab, uncheck "Initialize this repository with a README." Your first git push will just work.
  • Clone first, then add files โ€” skip git init entirely. Clone the remote repo and drop your files into the cloned directory.
  • If you must init locally โ€” add the remote, fetch, and merge before making any local commits.
# The safe workflow when starting fresh with an existing remote
git clone https://github.com/username/repo.git
cd repo
# Add your project files here
git add .
git commit -m "Initial project files"
git push

Related Error Notes