git subtree: possible to change subtree branch/path in a forked repository?

17,853

If you used git subtree (and not git submodule) to create the subtree, then it's just a normal dir. To switch it to another branch, just delete it and recreate the subtree from the new branch. This is:

git rm <subtree>
git commit
git subtree add --prefix=<subtree> <repository_url> <branch>

That should work without problems.

Share:
17,853
LearnCocos2D
Author by

LearnCocos2D

Author of the Learn SpriteBuilder book published by Apress. Developer of OpenGW, a game world simulation engines / entity component system with Model-View-Controller architecture that can be used with any rendering engine. Developer of Kobold Kit, the Sprite Kit game engine with tilemap support, game components, scheduling and event dispatching, and all the other things missing from Sprite Kit. Developer of KoboldTouch (commercial) and Kobold2D (free), both game engines build on cocos2d-iphone. KoboldTouch adds MVC and component support and even improves cocos2d's own features, such as Tilemap rendering. Author of the Learn Cocos2D book series published by Apress. The latest edition "Learn cocos2d 2" uses cocos2d 2.0 with ARC enabled and also covers Kobold2D. Steffen's main website is Learn Cocos2D.

Updated on June 21, 2022

Comments

  • LearnCocos2D
    LearnCocos2D almost 2 years

    In a repository A the folder sub is included as git subtree of the repository S - pointing to master branch.

    I have forked repository A into F. Now I want to do one of the following in F:

    • change sub to use a different branch of S (ie develop branch)
    • or: change sub to use a different repository altogether

    Is either one of these possible, and if so, how? Will there be any side effects I should know of?

    And how can I make sure my subtree change won't be updated in repository A when I merge my changes (pull request)? I mean besides isolating commits.

  • LearnCocos2D
    LearnCocos2D over 10 years
    and that won't be synched upstream? I guess I'll give that a try then.
  • elmart
    elmart over 10 years
    It won't be synced upstream. The <repository_url> is not saved anywhere; it's just used for the command to know where to get the files from. After that, you get a normal subdir within your repository.
  • LearnCocos2D
    LearnCocos2D over 10 years
    Ah ok, I see that now ... subtree simply "copies" the state of a commit of another repository, as if I had copied it manually (but preserving the commit history and what not). So that doesn't change my original problem of not synchronizing a specific folder between two repositories, but sufficiently answers this question.
  • Jorge Orpinel Pérez
    Jorge Orpinel Pérez over 9 years
    Note: I think it's always best to add a remote before adding and pulling a new subtree. In my experience it works much better and can't be done after you add the subtree. See stackoverflow.com/questions/16829401/…
  • Skulas
    Skulas about 2 years
    Since subtree is a folder you need to add -r to recurse it: git rm -r <subtree>