22

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

Community
  • 1
  • 1
Matthijs
  • 501
  • 1
  • 3
  • 12
  • 4
    As that post says, the error itself is new in Git version 2.9. Older versions of Git merged unrelated histories without complaint (and without any sensible merge base, though that's fine for unrelated subtrees). The subtree scripts rely on the old behavior. – torek Sep 01 '16 at 21:30
  • 6
    It looks to me like `--allow-unrelated-histories` is a parameter to `git merge` instead of `git subtree` because it's not found in `git-subtree(1)` but in `git-merge(1)` – das_j Oct 28 '16 at 17:34
  • 2
    I'm having the same problem. `--allow-unrelated-histories` seems wrong to me too, but I guess it'll have to do. – beane Mar 02 '18 at 23:48

5 Answers5

45

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

PencilBow
  • 979
  • 11
  • 17
  • Thanks! This was my issue as I had apparently copied the folder from another project – Cameron May 06 '19 at 18:22
  • When i use this command, i am always getting conflicts , even when i don't change anything in my local. Can you suggest how to resolve that and avoid unnecessary conflicts ? – Rohit Tailor Feb 24 '23 at 17:17
5

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.

Cyrus
  • 59
  • 1
  • 2
  • Squash is a useful option when retrieving a large repo. Removing the subtree and adding it back without the subtree can be avoided by using @PencilBow's solution. – Benoit Duffez Jan 02 '19 at 10:30
5

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.

leoschet
  • 1,697
  • 17
  • 33
2

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

daurnimator
  • 4,091
  • 18
  • 34
  • 2
    What git version did this finally make it into? – joeybladb Aug 02 '17 at 00:20
  • 5
    That particular commit landed in git v2.9.3 and AFAICT is not the solution to the OP's issue. It affects the behavior of `--rejoin`, which only applies to the `subtree split` command. – ZachB Nov 06 '18 at 21:35
0

I am not expert.

But I found something. I was originally trying just a normal pull. But I found this specific subtree pull.

git subtree pull --prefix myPrefixname https://github.com/subTreeRepo.git master --squash

where master is the branch name of course

(from

https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/

)

granadaCoder
  • 26,328
  • 10
  • 113
  • 146