2

First I wrongly copied another repo source straight to one bigger project, in a subdir. The original repo and subdir were changed both ever since that time. Now I have knowledge of git-subtree, I want to make the subdir as a real subtree so that I can easily sync the change from original repo.

I tried 'git-subtree split', then pull latest source from original repo, it just worked. However, I don't want the whole history, but '--squash' didn't work for 'split', here's my commands

git subtree split -P xxx-dir --onto==1940032
git remote add xxx https://github.com....
git fetch xxx
git subtree pull -P xxx-dir xxx master
# the pull worked, but whole history of xxx was imported too. '--squash' didn't work for above 'pull'

git subtree merge -P xxx-dir xxx/master --squash
# Can't squash-merge: 'xxx-dir' was never added. 

Looked like squash only works for subtree added but not split.

Any idea? Thanks.

fifth
  • 4,249
  • 9
  • 45
  • 62

2 Answers2

2

I was confused by git-subtree and git subtree merge

they are different things but maybe git-subtree is a kind of wrapper of some git subtree merge stuff https://git-scm.com/book/en/v1/Git-Tools-Subtree-Merging

This is a good answer about the differences between the two:
What is the difference between git subtree merge and git-subtree

as far as I understood the easiest to do what you is to

1. delete the xxx-dir directory and commit the change

rm -r xxx-dir
commit -am "xxx subtree"

2. add the subtree

git subtree add --prefix=xxx-dir --squash xxx master

The downside is that doing this changes the main repo history with 3 commits:

  1. the commit that deletes xxx-dir
  2. the commit that "imports" the subtree with its history squashed as one commit
  3. the commit that merges the imported subtree

As far as I understood this is a kind of limitation of git-subtree.

By the way, this should be the source code of git-subtree maybe someone more skilled than me can dig in it.

Ciao!

Community
  • 1
  • 1
nulll
  • 1,465
  • 1
  • 17
  • 28
0

Regarding:

git subtree pull -P xxx-dir xxx master
# the pull worked, but whole history of xxx was imported too. '--squash' didn't work for above 'pull'

It seems you didn't use --squash. Or are you trying to say you tried the command again, not as you've written it here, WITH --squash? git subtree pull --squash -P xxx-dir xxx master

... and Regarding:

git subtree merge -P xxx-dir xxx/master --squash
# Can't squash-merge: 'xxx-dir' was never added.

Which branch did you run this from? it seems like you're trying to merge in changes from the same branch you have checked out into a sub-folder of itself. Since the command fails, it can't merge. I don't think it's saying squash is the problem, it's just that it happens to be a squash style merge that is failing.

johnb003
  • 1,821
  • 16
  • 30