Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git subtree error "fatal: refusing to merge unrelated histories"

I'm trying to figure out how 'git subtree' works. I've followed all directions on this page, but I always get an error trying to merge the subtree project in my own repo ('Step 2'): fatal: refusing to merge unrelated histories.

I've read this post, and when I use the --allow-unrelated-histories option, it seems to work fine. However, I'm not sure whether I should use this...My impression is that the whole point of subtrees is to have unrelated histories within one repository, so it feels strange to have to add the option. Should I add it nevertheless, or am I doing something wrong?

I'm using git v2.9.3 on osx 10.11.6

like image 904
Matthijs Avatar asked Sep 01 '16 21:09

Matthijs


People also ask

How do I fix fatal refusing to merge unrelated histories?

The alternative (and longer) way of fixing the fatal: refusing to merge unrelated histories issues is to unstage your current commits, stash them, clone your required remote repository, and then place your stashed branch contents into the new clone.

What is refusing to merge unrelated histories?

Start Learning. 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).

How do you combine two unrelated histories?

To combine two separate Git repositories into one, add the repository to merge in as a remote to the repository to merge into. Then, combine their histories by merging while using the --allow-unrelated-histories command line option.

How do I merge unrelated branches?

In fact, all you need to do to merge unrelated branches is to use the flag --allow-unrelated-histories . This tells Git to combine all the files and commits of both unrelated branches into one branch, as long as there are no file conflicts.


4 Answers

If the subtree was added using --squash, you need to also use --squash when you pull

git subtree pull --prefix=<folder-goes-here> <remote-goes-here> <branch-goes-here> --squash

like image 136
PencilBow Avatar answered Sep 17 '22 17:09

PencilBow


I've been struggling with this for a while and think I found the solution.

I'm pretty new to git so forgive me if I use the wrong names for things.

The problem may have come from you using the --squash option when you ran the git subtree add command.

Try removing the subtree (remove the remote repository and delete all the local files, commit, and push). Then redo the subtree add without the --squash option.

I then jumped over to my subtree repository, made some changes, committed and pushed, then hopped back to my main superproject repository and did a git subtree pull. It gave me some error about the working tree having modifications. To get around that I did a git checkout master, then a git push, then tried the subtree pull again. It worked.

Hope that helps.

like image 36
Cyrus Avatar answered Sep 19 '22 17:09

Cyrus


What worked for me was using git merge with subtree strategy:

git merge -s subtree -Xsubtree="$prefix" subremote/branch --allow-unrelated-histories

git subtree uses something similar to that under the hood.

like image 39
leoschet Avatar answered Sep 20 '22 17:09

leoschet


This was a bug in git, it was fixed in https://github.com/git/git/commit/0f12c7d4d175bb0566208213b1b340b6794f305c

See also: https://www.spinics.net/lists/git/msg282340.html

like image 25
daurnimator Avatar answered Sep 21 '22 17:09

daurnimator