Resolving Git ‘fatal: refusing to merge unrelated histories’ on Ubuntu 22.04 LTS
Learn why Git refuses to merge unrelated histories and how to resolve this common issue when pulling or merging repositories on Ubuntu 22.04 LTS.
Learn why Git refuses to merge unrelated histories and how to resolve this common issue when pulling or merging repositories on Ubuntu 22.04 LTS.
Introduction
As a seasoned Systems Administrator or DevOps engineer working with Git, you've likely encountered the "fatal: refusing to merge unrelated histories" error. This issue typically surfaces when attempting to pull updates from a remote repository or merge two branches that Git perceives as having completely separate, independent origins. While initially frustrating, this is a crucial safety mechanism introduced in Git 2.9 to prevent accidental merging of projects that genuinely have no common ancestor, thereby safeguarding your repository's integrity and preventing potential data loss.
This guide will walk you through understanding the root causes of this error on your Ubuntu 22.04 LTS server and provide a clear, step-by-step resolution to merge your repositories correctly.
Symptom & Error Signature
When you attempt to pull changes from a remote repository, for example using git pull origin main or git pull origin master, or directly merge a remote branch after fetching, you will observe output similar to the following in your terminal:
$ git pull origin main
From https://github.com/your-org/your-repo
* branch main -> FETCH_HEAD
fatal: refusing to merge unrelated histories
Alternatively, if you've fetched the remote changes and are attempting a direct merge:
$ git fetch origin
$ git merge origin/main
fatal: refusing to merge unrelated histories
Root Cause Analysis
The "fatal: refusing to merge unrelated histories" error is Git's way of telling you that the two branches you are trying to merge (e.g., your local main and the remote origin/main) have no common commit ancestor in their history. Git's default merge strategy requires a common base commit to calculate a three-way merge, ensuring that changes from both sides are correctly integrated. Without a common ancestor, Git cannot determine a logical merge base, and thus, it refuses to proceed to prevent an incorrect or destructive merge.
This situation commonly arises in the following scenarios:
- Local
git initthen remote pull: You initialized an empty Git repository locally usinggit init, added some files, and perhaps made an initial commit. Meanwhile, a remote repository (e.g., on GitHub, GitLab, or a self-hosted Git server) was created for the same project, often including an initial commit (e.g., aREADME.md,.gitignore, orLICENSEfile) during its creation. When you then try togit pullfrom this remote into your local repo, Git sees two distinct initial commits with no shared history. - Cloning an empty repo, adding content, then linking to a different (populated) remote: Less common, but can happen if you clone an empty repository, start development, then later decide to link it to a different remote URL which already has existing commits.
- Two separate projects being merged into one: If you have two completely independent projects, each with its own Git history, and you decide to combine them, you will encounter this error when attempting to merge their respective
mainbranches.
In essence, Git views these histories as parallel universes that have never intersected, and it requires explicit instruction to bridge them.
Step-by-Step Resolution
The primary solution involves explicitly instructing Git to allow the merge of unrelated histories. This is done using the --allow-unrelated-histories flag.
1. Understand the Context and Verify Repositories
Before proceeding, it is critical to confirm that the remote repository you are attempting to merge from is indeed the correct one and that you genuinely intend to combine its history with your local repository. This flag should only be used when you are certain that the two histories should logically be considered part of the same project.
Check your remote URL:
git remote -vEnsure the URL points to the intended remote repository.
Inspect remote history (optional but recommended): If possible, you might want to briefly inspect the remote repository's history to ensure it looks as expected, especially if you suspect a misconfiguration. You can do this via the web interface of your Git hosting provider (GitHub, GitLab, etc.) or by temporarily cloning it to a separate directory.
2. The --allow-unrelated-histories Flag
This flag tells Git to create an initial merge commit that brings the two distinct histories together. It will establish a common base for all future merges.
Scenario A: Resolving git pull
If you encountered the error when running git pull, you can add the flag directly to your pull command:
git pull origin main --allow-unrelated-histories
origin: Your remote alias (oftenorigin).main: The branch you want to pull from the remote (e.g.,main,master,develop).--allow-unrelated-histories: The crucial flag.
This command will fetch changes from the remote
mainbranch and attempt to merge them into your current local branch. If there are conflicting files (e.g., both local and remote have aREADME.mdbut with different content), Git will pause the merge and prompt you to resolve these conflicts.
Scenario B: Resolving git merge after git fetch
If you first git fetch and then git merge, you would apply the flag to the merge command:
git fetch origin
git merge origin/main --allow-unrelated-histories
3. Resolving Merge Conflicts (If Applicable)
After running the git pull or git merge command with --allow-unrelated-histories, Git might report merge conflicts. This happens if the same lines or files have been modified differently in both the local and remote histories.
If conflicts occur, Git will tell you which files have conflicts. You can see the status with:
git status
For each conflicted file, you will need to manually edit it to resolve the differences. Git will mark conflict sections with markers like <<<<<<<, =======, >>>>>>>.
<<<<<<< HEAD
This is the content from my local repository.
My local changes.
=======
This is the content from the remote repository.
Remote changes.
>>>>>>> origin/main
Edit the file to include the desired final content, removing the Git conflict markers.
After resolving conflicts in all affected files, stage them:
git add .
Then, complete the merge commit:
git commit -m "Merge remote main branch with local history --allow-unrelated-histories"
Git will often pre-populate the commit message for a merge. You can accept it or modify it.
4. Pushing Changes to the Remote
Once the merge is complete locally, and any conflicts are resolved and committed, your local repository now has a unified history. You can then push these changes back to the remote repository.
git push origin main
This will update the remote main branch with the new merge commit, making the combined history available to all collaborators. Subsequent git pull and git push operations on this branch will now function normally without the "unrelated histories" error, as a common ancestor has been established.
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.