What is The “fatal: refusing to merge unrelated histories” Git error?

The "fatal: refusing to merge unrelated histories" error in Git occurs when you try to merge two branches that do not share a common commit history. This scenario often arises in situations such as:

  1. Merging Two Independent Repositories: This happens when you attempt to merge two completely separate projects (repositories) that started their lives independently of each other, without any shared history.

  2. Repository Reinitialization: If a repository was re-initialized (i.e., started afresh with a new git init command) but needs to pull or merge changes from a backup or a previous repository state, the histories would be seen as unrelated.

  3. Squashed or Rebased Commits: If the history of one branch has been rewritten (for example, through rebasing or squashing commits), Git may fail to recognize the related history that it once shared with another branch.

How Git Handles Histories

Git is meticulous about tracking the lineage of commits for integrity and version control purposes. Each commit in a Git repository has a parent commit (except for the very first one), creating a chain that traces back to the initial commit. When you attempt to merge two branches, Git looks for a common base commit to correctly integrate the changes from each branch. Without this common commit, Git cannot safely perform the merge as it cannot ascertain how the changes from the two histories should interact.

Resolving the Error

To resolve this error and force Git to merge the two unrelated histories, you can use the --allow-unrelated-histories option of the git merge command. Here’s how to do it:

git merge other-branch --allow-unrelated-histories

Steps to Follow:

  1. Switch to the Correct Branch: Ensure you are on the branch that you want to merge into.

    git checkout main
  2. Merge with the Allow Unrelated Histories Flag:

    git merge feature-branch --allow-unrelated-histories
  3. Resolve Any Conflicts: If there are conflicts, Git will ask you to resolve them. Open the conflicting files and make the necessary changes.

  4. Commit the Merge: Once all conflicts are resolved, add the resolved files to the staging area using git add, and then commit them:

    git add . git commit -m "Merge feature-branch into main with unrelated histories resolved"

Considerations

  • Be Cautious: Using --allow-unrelated-histories should be done with caution. Understand why the histories are considered unrelated and consider if merging them is indeed the correct action.

  • Backup Your Work: Before performing operations that can significantly alter the history or content of your repository, consider creating backups of your current state.

  • Use in Appropriate Context: Typically, merging unrelated histories is not a regular practice and might indicate an issue with how branches or repositories are managed. It’s appropriate in specific cases like consolidating two independent projects into a single repository.

Understanding and resolving the "fatal: refusing to merge unrelated histories" error involves recognizing the importance of commit history in Git and carefully managing how different histories are integrated. This ensures the stability and traceability of changes in your projects.

TAGS
System Design Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is Shopify skill?
What happens in 2nd round of technical interview?
Which company has toughest interview for software engineer?
What are the tips for acing algorithm design interviews?
What are the most common bottlenecks in large-scale system design?
Discover the most common bottlenecks in system design — from database overload to network latency — and learn how to detect and fix them in FAANG-style system design interviews.
Why do you want to join GitLab?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.