Git & CI/CD Intermediate

Fatal: Refusing to Merge Unrelated Histories in Git on CentOS Stream / Rocky Linux

Resolve 'fatal: refusing to merge unrelated histories' when merging Git repositories on CentOS Stream or Rocky Linux.

👨‍💻
Senior Systems Architect • Verified in Staging Labs

Resolve 'fatal: refusing to merge unrelated histories' when merging Git repositories on CentOS Stream or Rocky Linux.

When managing codebases or deploying applications on CentOS Stream or Rocky Linux, encountering the "fatal: refusing to merge unrelated histories" error in Git can be a bewildering experience. This guide provides a deep dive into why this error occurs and, more importantly, how to safely resolve it, ensuring your development and deployment workflows remain smooth and efficient.

Symptom & Error Signature

You typically encounter this error when attempting to combine two Git repositories that, from Git's perspective, have no common ancestor in their commit history. This often happens after initializing a new local repository and then trying to pull or merge from an existing remote repository, or vice versa.

The error message will look similar to this in your terminal:

$ git pull origin main
From https://example.com/your-repo.git
 * branch            main       -> FETCH_HEAD
fatal: refusing to merge unrelated histories

Or, if you are explicitly trying to merge:

$ git merge origin/main
fatal: refusing to merge unrelated histories

Root Cause Analysis

Git's "refusing to merge unrelated histories" error is a crucial safety mechanism designed to prevent accidental merges of two entirely separate projects that happen to share the same branch name (e.g., main or master). Git believes these repositories evolved independently and forcing a merge without explicit instruction could lead to significant data loss or an unmanageable codebase.

The underlying reasons for this error commonly include:

  1. Separate Initial Commits: You created a local repository with git init and made an initial commit. Meanwhile, a remote repository was also created (e.g., on GitHub, GitLab, or your own Git server), often with its own initial commit (e.g., a README.md, .gitignore, or license file). When you try to git pull the remote's main branch into your local main branch, Git sees two distinct histories with no shared ancestry.
  2. Repository Re-initialization: A project's .git directory was accidentally deleted or corrupted, leading to the repository being re-initialized (git init) and a new commit history starting from scratch. When you then attempt to sync with the original remote, Git detects the history mismatch.
  3. Restoring from Backup: A project was restored from a backup that either didn't include the .git directory, or the backup was from a state where the .git directory was fresh, causing a divergence from the current remote history.
  4. Incorrect Cloning/Setup: While less common for this specific error, sometimes repositories are manually created and pushed or pulled in ways that bypass the typical git clone workflow, leading to history mismatches.

Git is trying to protect you from combining two truly distinct projects that might share file names but are fundamentally different.

Step-by-Step Resolution

The solution involves explicitly telling Git that you understand the histories are unrelated but still wish to merge them. This is achieved using the --allow-unrelated-histories flag.

1. Add the Remote Repository (if not already done)

If you're starting with a fresh local repository and want to connect it to an existing remote, first add the remote. If you already have origin configured, you can skip this step.

$ git remote add origin <repository-url>

Replace <repository-url> with the SSH or HTTPS URL of your remote Git repository. For example: https://github.com/youruser/yourrepo.git or [email protected]:youruser/yourrepo.git.

2. Pull with --allow-unrelated-histories

This is the core step to resolve the error. You will fetch the remote changes and merge them into your current local branch, explicitly permitting the merge of unrelated histories.

$ git pull origin <branch-name> --allow-unrelated-histories

Replace <branch-name> with the name of the branch you want to pull (e.g., main, master, develop).

Using --allow-unrelated-histories tells Git to bypass its safety check. This means Git will attempt to merge the root commits of both histories. If there are identically named files with different content in the initial commits of both repositories, Git will treat them as conflicts, and you will need to resolve them manually. Review changes carefully!

3. Resolve Merge Conflicts (if any)

After running the git pull command, Git will likely open your default text editor (e.g., Vim, Nano) to prompt you for a merge commit message. This happens even if there are no file conflicts, as Git needs to record the merge.

If there are file conflicts (which is common when histories are truly unrelated and both sides have files like README.md or .gitignore), Git will pause the merge and output messages indicating which files are conflicted.

Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

To resolve conflicts:

  • Open the conflicted files in your preferred editor.
  • Look for conflict markers: <<<<<<<, =======, >>>>>>>.
  • Decide which version of the code to keep (your local, the remote's, or a combination).
  • Remove the conflict markers.

After resolving all conflicts, stage the files:

$ git add .

4. Complete the Merge Commit

Once all conflicts are resolved and staged, commit the merge:

$ git commit -m "Merge remote branch '<branch-name>' with --allow-unrelated-histories"

Take your time to review the merged changes before committing. This merge introduces a new commit that connects two previously disparate histories. Ensure all intended files are present and unwanted files are excluded.

5. Push Your Changes to the Remote

Finally, push your local, now merged, branch back to the remote repository. This makes the new merged history available to others.

$ git push origin <branch-name>

Example Workflow: Bringing an Existing Project to a New Remote

Let's assume you have a local project directory /var/www/myproject with existing code and git init and some commits, and you've created an empty remote repository on GitLab.

# 1. Navigate to your project directory
cd /var/www/myproject

# 2. Initialize Git locally and make an initial commit (if not already done)
# If you already have a local history, skip this.
# git init
# git add .
# git commit -m "Initial local commit"

# 3. Add the remote repository
git remote add origin [email protected]:youruser/myproject.git

# 4. Pull the remote branch, allowing unrelated histories
# The remote might have an initial README.md or .gitignore, which would cause the error.
git pull origin main --allow-unrelated-histories

# 5. Resolve any conflicts (e.g., README.md)
# Example: edit README.md to combine content, then add it.
git add README.md

# 6. Commit the merge
git commit -m "Initial merge of remote 'main' branch allowing unrelated histories"

# 7. Push your combined history to the remote
git push -u origin main # -u sets upstream tracking

This comprehensive approach will successfully merge your disparate Git histories on your CentOS Stream or Rocky Linux system, allowing you to proceed with your normal development and deployment cycles.

👨‍💻

Johnathon Wheeler

Senior Systems Architect & DevOps Engineer • Austin, TX

Connect on LinkedIn →

Johnathon has over 16 years of hands-on experience designing, debugging, and scaling Linux web hosting stacks, container clusters, and high-availability database architectures. Every guide on ButItWorkedLocal is independently tested against Debian 12, Ubuntu 24.04/22.04 LTS, Rocky Linux, and Docker environments to guarantee reproducibility in production.

🛡️

Our Production Verification Guarantee

Encountering a bug not covered here or running a non-standard kernel configuration? Our solutions are continually refined against real production incidents. Submit an environment trace for our editorial team to replicate.