6

Assuming I have already added a new remote "foo", and pulled a subtree, squashed, with branch master.

$ git remote add -f foo some-repo.git
$ git subtree add --prefix=foo --squash foo master

From the articles I have read (here, and here, etc), this appears to be the common way to switch branches. In this case, to branch "bar":

$ git rm -r foo
rm 'foo/file'.
...
$ git commit -m "Delete foo on branch 'master' to switch to branch 'bar'."
1350 files changed, 144703 deletions(-)
delete mode 100644 foo/file
...
$ git subtree add --prefix=foo --squash foo bar
git fetch foo master
From some-repo.git
 * branch            master     -> FETCH_HEAD
Added dir 'foo'

But this feels dirty. Why not just use "git subtree pull" which already has a branch argument (no rm/commit/add)?

$ git subtree pull --prefix=foo --squash foo bar
From some-repo.git
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 foo/file                                                        |  2 ++
 ... smaller changeset output ...

 3 files changed, 41 insertions(+)
 ...

When I have tested both locally, I see no differences between the subtree dirs (./foo). Why is everyone recommending the way that adds more complex changesets and commits (removing and re-adding)? What am I missing, maybe some edge cases?

jmar
  • 452
  • 2
  • 14

1 Answers1

0

git subtree pull was introduced in commit 13648af5 in April 2009 for Git 1.7.11 by Avery Pennarun apenwarr.

Add 'git subtree merge' and 'git subtree pull'.

These are simple shortcuts for 'git merge -s subtree' and 'git pull -s subtree', but it makes sense to have it all in one command.

You can see in its associated test

-git merge -s subtree subproj-merge-split3
+git subtree pull --prefix=subdir ../subproj subproj-merge-split3

This is not the same as rm/subtree add, which remains better suited to switch branch.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This didn't really answer my question (why one method over another for switching branches), but since I'm getting no other answers, you get the bounty. – jmar Jul 26 '18 at 21:26