You can use --allow-unrelated-histories to force the merge to happen.

The reason behind this is that default behavior has changed since Git 2.9:

"git merge" used to allow merging two branches that have no common base by default, which led to a brand new history of an existing project created and then get pulled by an unsuspecting maintainer, which allowed an unnecessary parallel history merged into the existing project. The command has been taught not to allow this by default, with an escape hatch --allow-unrelated-histories option to be used in a rare event that merges histories of two projects that started their lives independently.

See the Git release changelog for more information.

More information can be found in this answer.

Answer from blue112 on Stack Overflow
🌐
Git Tower
git-tower.com › learn › git faq › how to fix "fatal: refusing to merge unrelated histories" in git
How to Fix "fatal: refusing to merge unrelated histories" in Git | Learn Version Control with Git
17 hours ago - git merge --allow-unrelated-histories <branch-name> This option forces Git to proceed with the merge even if it cannot find a common ancestor between the branches.
Discussions

Merging unrelated histories
Since you changed the original commit with your ament, it makes sense to force push now, as you wanted to change the original commit. git push --force-with-lease More on reddit.com
🌐 r/git
12
6
November 21, 2020
What is The “fatal: refusing to merge unrelated histories” Git error?
Without this common commit, Git cannot safely perform the merge as it cannot ascertain how the changes from the two histories should interact. 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. More on designgurus.io
🌐 designgurus.io
1
10
June 25, 2024
git pull - Git is refusing to merge unrelated histories. What are 'unrelated histories'? - Stack Overflow
In this situation, git merge or pull request will unable to track where you made changes to add with the remote project. Hence, " refusing to merge unrelated histories"- error occurs. In this situation, if you try to force merge by following commands, More on stackoverflow.com
🌐 stackoverflow.com
How to deal with "refusing to merge unrelated histories" error
Short version of my question : ... my computer died suddenly and I bought a new one. Now Github refuses to connect the local repo from my new computer to the online repo saying “refusing to merge unrelated histories”. What do I do ?... More on github.com
🌐 github.com
19
104
🌐
Reddit
reddit.com › r/git › merging unrelated histories
r/git on Reddit: Merging unrelated histories
November 21, 2020 -

I created the following repository on GitHub: https://github.com/james/test.git.

Then I proceeded with:

user@guest:~/.../gits$ git config --global init.defaultBranch main
user@guest:~/.../gits$ mkdir test && cd test
user@guest:~/.../test$ echo "# test" >> README.md
user@guest:~/.../test$ git init
user@guest:~/.../test$ git add README.md
user@guest:~/.../test$ git commit -m "First commit"
user@guest:~/.../test$ git remote add origin https://github.com/james/test.git
user@guest:~/.../test$ git push -u origin main

And then:

user@guest:~/.../test$ git commit --amend
user@guest:~/.../test$ git log --oneline --graph --all
* ca82a6d (HEAD -> main) First commit
* 085bb3b (origin/main) First commit
user@guest:~/.../test$ git merge origin/main
fatal: refusing to merge unrelated histories

What did I do wrong and what is the best solution in this case?

🌐
Graphite
graphite.com › guides › how-to-resolve-git-error-refusing-to-merge-unrelated-histories
How to resolve the Git error "refusing to merge unrelated histories"
Attempt to merge the remote branch (e.g., main) into your local branch (e.g., main) using the --allow-unrelated-histories flag: ... This command tells Git to permit the merge despite the lack of shared history between the branches.
🌐
GitHub
gist.github.com › kingluddite › 7ec82fdfa3700427efa0e5b97196dea6
unrelated-histories.md · GitHub
How to Resolve "Refusing to merge unrelated histories": If you're certain that you want to merge the unrelated histories, you can force Git to do so with the --allow-unrelated-histories flag:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-merge-unrelated-histories-in-git
How to Merge Unrelated Histories in Git? - GeeksforGeeks
July 23, 2025 - This safeguard prevents accidental merges of entirely different projects. To bypass this, you need to explicitly tell Git to allow the merge of these unrelated histories.
🌐
Medium
medium.com › @jkc5186 › fatal-refusing-to-merge-unrelated-histories-git-github-aef8a171a53d
“fatal: refusing to merge unrelated histories” -(git-github) | by Jeevan KC | Medium
July 8, 2023 - Git considers these histories unrelated and, by default, refuses to merge them to avoid potentially conflicting changes. ... Pull with --allow-unrelated-histories: You can force the merge by using the --allow-unrelated-histories flag.
🌐
OneUptime
oneuptime.com › home › blog › how to fix 'fatal: refusing to merge unrelated histories'
How to Fix 'Fatal: refusing to merge unrelated histories'
January 24, 2026 - # Instead of: git init git remote ... Common legitimate cases: ... # Verify you have the right remote before forcing git remote -v git fetch origin git log origin/main --oneline # Look at what you're merging...
Top answer
1 of 7
300

I think you have committed in the remote repository, and when you pull, this error happens.

Use this command:

git pull origin main --allow-unrelated-histories
git merge origin origin/main
2 of 7
65

I ran into a similar problem where I brought in a branch from a second remote and wanted to merge with a branch from the first remote. This is different from the existing answers as I'm not using --allow-unrelated-histories on the pull, but on the merge.

git fetch origin main
git checkout main
git merge --allow-unrelated-histories myfunnybranch

This fetches and checks out the main branch and merges the myfunnybranch into it. By using git fetch rather than git pull we distinguish between bringing code to our machine, and performing the difficult merge.

When NOT to perform an unrelated history merge

Warning: An "unrelated history" is a history that does not go back to a common source. In other words, myfunnybranch and main are derived from two completely different repositories.

If the two repositories are very similar or have lots of files in common, there is a good chance that one of the repos lost its history somewhere along the way. Whether or not this is the case, its probably best to avoid two histories leading to the same set of files. Instead, you'd like to keep the longer history and have a single commit bringing in any changes from the other repository. To achieve this goal, checkout the repository with the longer history and copy the files of the other one in, which should produce a small commit with few changes, which git status and git diff will summarize for you before you make the commit with git commit. For more details, this answer to another question, which was mentioned in a comment on another answer to this question.

In the OP's case (which was elaborated on in an edit to the question), there is likely no need for an unrelated merge. Perhaps the OP didn't clone the repo, or perhaps someone else pushed to the empty repo before the OP made their initial commit. Either way, since the OP is editing only a single file, it would be better to simply clone the repo fresh, copy the new file into the cloned repo, and add, commit, and push there.

When TO perform an unrelated history merge

You would only want to merge unrelated histories if the two repositories indeed share very little in common. For example, each repository contributes a different set of files during the merges.

In my case I wanted to merge two totally different repos that had files in different directories and I wanted to preserve the history of both of these. This was code that I thought should be together in a single repo but was originally written in two different repos.

While the OP appears to be adding a new file here, this is probably not a case where we need multiple histories.

As P. Ent notes in a comment, one case where it makes sense to merge unrelated histories is when you have performed a fair bit of development locally, and there really hasn't been much development on origin/main. If essentially all of the development was done locally on myfunnybranch, it might even make sense to run

git checkout myfunnybranch
git merge -s ours --allow-unrelated-histories main

This ignores all the work on the main branch, but keeps it in the history. Only the code in myfunnybranch is kept. Now merging myfunnybranch back into main should be easy.

In cases like these, git log --all --graph is useful to see what your local repository looks like before you push anything.

For example, after the git merge -s ours command suggested above, you might find your repo to look like

git log --all --oneline --graph
*   6c8188b (HEAD -> myfunnybranch) Merge branch 'main' into myfunnybranch
|\
| * b93aabd (origin/main, origin/HEAD, main) Initial commit on remote server
* f69e99b some work I did locally
* df5d8ae more local work
* da455d6 Initial commit that I made locally

In this case you could use

git diff 6c8188b f69e99b 

to confirm that your most recent commit, 6c8188b, really didn't change any of the work you did locally. It will print nothing if there are no changes.

Then, merging myfunnybranch back into main will result in main matching your local development, too. This is because from the main branch's perspective, commit 6c8188b makes a whole bunch of edits bringing the branch exactly in line with the other branch.

🌐
OpenReplay
blog.openreplay.com › openreplay blog › how to fix 'fatal: refusing to merge unrelated histories' during git rebase
How to Fix 'fatal: refusing to merge unrelated histories' During Git Rebase
December 2, 2024 - Yes, you can use `--allow-unrelated-histories` with `git pull` to fetch and merge changes from a remote repository with a different commit history.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-fix-git-refusing-to-merge-unrelated-histories-on-rebase
How to Fix - Git Refusing to Merge Unrelated Histories on Rebase? - GeeksforGeeks
May 27, 2024 - To resolve this error, you can instruct Git to allow merging unrelated histories using the --allow-unrelated-histories flag.
🌐
C# Corner
c-sharpcorner.com › article › git-error-refusing-to-merge-unrelated-histories
Git - Error - Refusing To Merge Unrelated Histories
August 15, 2022 - An explanation of the problem and instructions are at [2]. We need to run “git merge <branch-name> --allow-unrelated-histories." So, you can open GitBash from SourceTree (Terminal button), and execute the command.
🌐
Baeldung
baeldung.com › home › git › how to fix git “refusing to merge unrelated histories”
How to Fix Git “Refusing to Merge Unrelated Histories” | Baeldung on Ops
February 6, 2024 - The –allow-unrelated-histories option will tell the Git that we allow merging branches with no common history base, which then should finish the merge without errors. We should note that for the Git version 2.9 or older, this option is not ...
🌐
LocalUsr
localusr.com › posts › 2022 › 01 › 2-how-to-merge-unrelated-histories
How to Merge Unrelated Histories in Git :: LocalUsr
January 12, 2022 - You do this by added the option --allow-unrelated-histories. git merge origin master --allow-unrelated-histories
🌐
DEV Community
dev.to › donovanrichardson › adding-a-new-remote-and-merging-unrelated-branches-1mkm
Adding a New Remote and Merging Unrelated Branches - DEV Community
August 24, 2020 - But this doesn't mean that Git cannot perform any merge. In fact, all you need to do to merge unrelated branches is to use the flag --allow-unrelated-histories.
🌐
Namehero
namehero.com › blog › resolving-the-fatal-refusing-to-merge-unrelated-histories-error
Resolving the "Fatal: Refusing to Merge Unrelated Histories" Error
April 9, 2025 - Now when you try and merge these ... they work together and don’t mess with the same files. As before, use the overriding flag to force the merge....
🌐
CICube
cicube.io › blog › refusing-to-merge-unrelated-histories
What is refusing to merge unrelated histories Error? | CICube
December 10, 2024 - The easiest way to do this is by using the --allow-unrelated-histories flag. Here's what happens when you use it: As you can see, Git tries to combine both histories together and auto resolves some conflicts.