'Git refusing to merge unrelated histories on rebase

During git rebase origin/development the following error message is shown from Git:

fatal: refusing to merge unrelated histories
Error redoing merge 1234deadbeef1234deadbeef

My Git version is 2.9.0. It used to work fine in the previous version.

How can I continue this rebase allowing unrelated histories with the forced flag introduced in the new release?



Solution 1:[1]

The 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.

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

Solution 2:[2]

In my case, the error was just fatal: refusing to merge unrelated histories on every try, especially the first pull request after remotely adding a Git repository.

Using the --allow-unrelated-histories flag worked with a pull request in this way:

git pull origin branchname --allow-unrelated-histories

As per 2.9.0 release notes - git pull has been taught to pass the --allow-unrelated-histories option to underlying git merge

Solution 3:[3]

Try the following command:

git pull origin master --allow-unrelated-histories

This should solve your problem.

Solution 4:[4]

I got this error when I set up a local repository first. Then I went to GitHub and created a new repository. Then I ran

git remote add origin <repository url>

When I tried to push or pull, I got the same fatal: unrelated_histories error every time.

Here is how I fixed it:

git pull origin master --allow-unrelated-histories
git merge origin origin/master
... add and commit here...
git push origin master

Solution 5:[5]

For this, enter the command:

git pull origin branchname --allow-unrelated-histories

For example,

git pull origin master --allow-unrelated-histories

Reference:

GitHub unrelated histories issue

Solution 6:[6]

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

You will be routed to a Vim edit window:

  • Insert commit message
  • Then press Esc (to exit "Insert" mode), then : (colon), then x (small "x") and finally hit Enter to get out of Vim
  • git push --set-upstream origin <branch>

Solution 7:[7]

I ran this command and issue got resolved.

git pull origin branchName --allow-unrelated-histories

Check out this page for more info.

Solution 8:[8]

I had the same problem. Try this:

git pull origin master --allow-unrelated-histories 

git push origin master

Solution 9:[9]

1. Solve the problem

Following Error when do a git pull origin master:

fatal: refusing to merge unrelated histories

Run one of the below Commands

 # It could be master
 git pull origin master --allow-unrelated-histories
 # Or main
 git pull origin main --allow-unrelated-histories
 # Or just try with origin 
 git pull origin main --allow-unrelated-histories

If it opens a nano editor you can just save and close with CTRL + x

Now push the changes on local

 git push

2. Meaning

  • The Error:

The “fatal: refusing to merge unrelated histories” Git error occurs when two unrelated projects are merged (i.e., projects that are not aware of each other’s existence and have mismatching commit histories).

By default, git merge command refuses to merge histories that do not share a common ancestor. This option can be used to override this safety when merging histories of two projects that started their lives independently. As that is a very rare occasion, no configuration variable to enable this by default exists and will not be added.

enter image description here


References


More on Stack Overflow

Solution 10:[10]

For Android Studio and IntelliJ:

First, commit everything and resolve any conflicts.

Then open the terminal from below of IDE and enter:

git pull origin master --allow-unrelated-histories

Now you can push.

Solution 11:[11]

Try git pull --rebase development

Solution 12:[12]

Since all the other answers are not actually answering the question, here is a solution inspired by this answer on a related question.

So you get your error doing git rebase:

$ git rebase origin/development
fatal: refusing to merge unrelated histories
Error redoing merge 1234deadbeef1234deadbeef

This error doesn't actually cancel the rebase, but you are now in the middle of it:

$ git status
interactive rebase in progress; onto 4321beefdead
Last command done (1 command done):
   pick 1234deadbeef1234deadbeef test merge commit

So you can now do the merge by hand. Find out the parent commits of the original merge commit:

$ git log -1 1234deadbeef1234deadbeef
commit 1234deadbeef1234deadbeef
Merge: 111111111 222222222
Author: Hans Dampf
Date:   Wed Jun 6 18:04:35 2018 +0200

    test merge commit

Find out which of the two merge parents is the one that was merged into the current one (probably the second one, verify with git log 222222222), and then do the merge by hand, copying the commit message of the original merge commit:

$ git merge --allow-unrelated 222222222 --no-commit
Automatic merge went well; stopped before committing as requested
$ git commit -C 1234deadbeef1234deadbeef
[detached HEAD 909af09ec] test merge commit
 Date: Wed Jun 6 18:04:35 2018 +0200
$ git rebase --continue
Successfully rebased and updated refs/heads/test-branch.

Solution 13:[13]

WARNING THIS WILL POTENTIALLY OVERWRITE THE REMOTE REPOSITORY

This worked for me:

git push origin master --force

Solution 14:[14]

Firstly pull the remote changes to your local using the following command:

git pull origin branchname --allow-unrelated-histories

** branchname is master in my case.

When the pull command done, conflict occurs. You should solve the conflicts. I use Android Studio to solve conflicts. enter image description here

When conflicts solved, merge is done!

Now you can safely push.

Solution 15:[15]

I had the same problem. The problem is remote had something preventing this.

I first created a local repository. I added a LICENSE and README.md file to my local and committed.

Then I wanted a remote repository so I created one on GitHub. Here I made a mistake of checking "Initialize this repository with a README", which created a README.md in remote too.

So now when I ran

git push --set-upstream origin master

I got:

error: failed to push some refs to 'https://github.com/lokeshub/myTODs.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes
(e.g. hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Now to overcome this I did

git pull origin master

Which resulted in the below error:

From https://github.com/lokeshub/myTODs
branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories**

I tried:

git pull origin master --allow-unrelated-histories

Result:

From https://github.com/lokeshub/myTODs
 * branch            master     -> FETCH_HEAD
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed;
fix conflicts and then commit the result.

Solution:

I removed the remote repository and created a new (I think only removing file README could have worked) and after that the below worked:

git remote rm origin
git remote add origin https://github.com/lokeshub/myTODOs.git
git push --set-upstream origin master

Solution 16:[16]

This usually happens when you commit first time to remote repository. As error clearly says "refusing to merge unrelated histories", we need to use --allow-unrelated-histories flag.

git pull origin master  --allow-unrelated-histories

Now there would be some conflicts which we have to solve manually. After that just commit the code and push it.

Solution 17:[17]

I tried git pull --allow-unrelated-histories didn't work, but what solves this issue for me was:

  1. I copied all the files on my desktop repository to another folder and then deleted the folder.

  2. Then I clone the repo again because it is a new project.

  3. When I copied my files again and push it worked like charm.

Solution 18:[18]

When doing a git pull, I got this message fatal: refusing to merge unrelated histories for a repo module where I hadn't updated the local copy for a while.

I ran this command just to refresh local from origin. I just wanted latest from remote and didn't need any local changes.

git reset --hard origin/master

This fixed it in my case.

Solution 19:[19]

I struggled with this as well, but I managed to find a workaround.

When you run into the error above, just cherry-pick the merge commit and then continue the rebase:

git cherry-pick -m 1 1234deadbeef1234deadbeef
git rebase --continue

Solution 20:[20]

For Googlers:

If you created a new repo on GitHub and accidentally initialized it with README or .gitignore files.

If you found yourself unable to merge or rebase because .git folder got corrupted.

Then:

  • Create a new folder
  • git clone
  • Paste all your files into this folder

Now the local and remote will have "related histories" and will merge or rebase happily.

Solution 21:[21]

I run into the same issue: This is what I've done:

git pull origin main --allow-unrelated-histories

I used VsCode to resolve merge conflicts then I did:

git commit -m "commit message"

git push origin main

Solution 22:[22]

For my case I wanted to merge unrelated history branch to my current:

git merge <-unrelated-history-branch-name> --allow-unrelated-histories

Solution 23:[23]

The error is resolved by toggling the allow-unrelated-histories switch. After a git pull or git merge command, add the following tag:

git pull origin master --allow-unrelated-histories

After then may be you will get conflict. So resolved the conflict and commit it. It's work for me.

Solution 24:[24]

The most voted answer doesn't solve this question, which is in the context of rebasing.

The only way to synchronize the two diverged branches is to merge them back together, resulting in an extra merge commit and two sets of commits that contain the same changes (the original ones, and the ones from your rebased branch). Needless to say, this is a very confusing situation.

So, before you run git rebase, always ask yourself, “Is anyone else looking at this branch?” If the answer is yes, take your hands off the keyboard and start thinking about a non-destructive way to make your changes (e.g., the git revert command). Otherwise, you’re safe to re-write history as much as you like.

Reference: https://www.atlassian.com/git/tutorials/merging-vs-rebasing#the-golden-rule-of-rebasing

Solution 25:[25]

I am using the rebase for years and I had never encountered such a problem. However, your first problem is, that you try to do it directly on the remote branch development from the remote repository, called origin. That is literally wrong because rebase is a dangerous command, that restructures the git history. Having said that, you should first try on your local repository and pushing it only, if it works for you as expected.

So, my usual rebase workflow looks like following (but please keep in mind, that you should not use rebase on branches, which you are not the only one committee. For such branches, use simply merge and resolve conflicts, if applicable):

  1. make sure you have a clean working tree (no uncommit changes)
  2. checkout to the branch you want to rebase onto (for instance, let's say it's master; as a one-line command): git checkout master && git pull origin master && git checkout development
  3. Do the actual rebase: git rebase master
  4. If it's done and everything works as expected, push it to your remote. For doing so, you need to force it, because the remote host already has the history in another order, the remote would answer with nothing to push. So, we need to say "my local version of the history is correct, overwrite everything on that remote branch using my local version of the history": git push -f origin development

As I already mentioned, keep in mind, that rebase manipulates the git history, that is usually a bad thing. However, it's possible to do that on branches, where no one else commits to. In order to keep the branch pull-able for the other developers, use another merge strategy like merge itself, squash or cherrypick. So, in other words: Rebase shouldn't be your tool on distributed development. It works fine for you if you are the only one who works on this repository.

We use the feature branch strategy. In this, I usually use rebase in order to get the "updates" from the other developers, that happened in the meantime on the master branch. Doing so, it reduces the size of commits that are visible in a pull request. Therefore, it makes it easier for the code reviewer to see my changes made in this feature branch.

Solution 26:[26]

I faced this error after force-pushing to origin/master a dev branch with a few hundreds of commits, by the Administrator, on the server side.

Well, what I just wanted is not to pull (fetch+merge) but just to align my local master to the remote origin master. Moving to a separate folder and doing a git clone is one approach, but I believe it is more elegant solution just to do a hard reset.

So my answer to this error, in this particular case, is none of the above. I just wanted this:

git reset --hard origin/master

Solution 27:[27]

Since you're neither able to push nor pull-and-push nor merge-pull-and-push: -

  1. You can create a new branch on the GitHub repository.
  2. And then: -
  3. git add .
  4. git commit -m 'commitName'
  5. And reference that branch in your current directory in Terminal.
  6. git branch -m master branchName
  7. git push -f origin branchName
  8. Your code will be pushed to a new branch.
  9. And then you can merge these 2 branches.

This worked for me.

Solution 28:[28]

fatal: refusing to merge unrelated histories may also be caused by a shallow clone b.c. the graft commit doesn't go far enough down to see the common base

Solution 29:[29]

I had same error, this command worked me

git pull gitlab master --allow-unrelated-histories

Note that gitlab might be origin or heroku in your case.

Solution 30:[30]

This also happens if you have a shallow git clone of the repository.

I recently encountered this problem in my CI/CD setup, none of the above solutions worked for me. I was building CI/CD pipeline to analyze code on MR creation against MR source branch, for that, I need to run analysis on the main branch once and then on the MR source branch merged with main, I got this error while trying to merge branches via git merge command.

Reason for this happening in CI/CD setup: Usually in CI/CD environment, git repositories are a shallow clone to speed up things, which doesn't include a full history of commits due to which when merging git may think that we are trying to merge unrelated branches which is in fact not true.

Solution: Convert shallow repositry to unshallow one using the following command:

git fetch --unshallow