24

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.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217

1 Answers1

43

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.

elmart
  • 2,324
  • 15
  • 17
  • and that won't be synched upstream? I guess I'll give that a try then. – CodeSmile Aug 30 '13 at 16:56
  • It won't be synced upstream. The `` 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. – elmart Aug 30 '13 at 17:01
  • 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. – CodeSmile Aug 30 '13 at 17:36
  • 1
    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 http://stackoverflow.com/questions/16829401/adding-git-subtree-from-a-branch – Jorge Orpinel Pérez Sep 02 '14 at 20:00
  • Since subtree is a folder you need to add -r to recurse it: `git rm -r ` – Skulas Jan 26 '22 at 08:17