且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

git子树:可以更改分支库中的子树分支/路径?

更新时间:2023-01-17 19:24:07

如果您使用 git subtree 而不是 git submodule )创建子树,那么它只是一个普通的目录。要将其切换到另一个分支,只需将其删除并从新分支中重新创建子树。这是:

  git rm< subtree> 
git commit
git subtree add --prefix =< subtree> &LT; repository_url&GT; &LT;分支&GT;

这应该没有问题。


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.

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.