在评论的帮助下,我找到了如下所示的解决方案。
0. 用公用文件夹的内容填充单独的存储库
将文件夹“public”的内容推送到新存储库:
cd public
git init --initial-branch=main
git remote add origin https://github.com/user/new_repository.git
git add .
git commit -m "Initial commit"
git push -u origin main
A. 使用单独的存储库作为子模块“public”
集成子模块
git submodule add 命令创建一个文件 .gitmodules ,其中包含
[submodule "public"]
path = public
url = https://github.com/user/new_repository.git
branch = main
新鲜克隆
如果其他人做了新鲜克隆主体工程中,递归的需要使用选项。否则子模块文件夹“public”的内容将为空。
提交子模块的更改
如果有人更改了子模块的内容,则需要分别提交/推送子模块和主项目。这似乎是子模块方法的一个缺点。
如果我更改子模块的文件,主项目不会将其识别为更改(提交对话框列表将为空)。一旦我使用子模块文件夹提交了更改,主项目就会识别出更改(子模块的新版本)。然后主要项目也需要提交。
TortoiseGit 的推送对话框的“Recurse submodule”选项的值为“Ondemand”:
因此,至少主模块项目和子模块项目都可以执行推送操作。 (如果有一个命令可以提交并推送两者,请告诉我。)
子模块使用的相关问题
如何“git克隆”一个存储库,包括它的子模块? https://stackoverflow.com/questions/3796927/how-do-i-git-clone-a-repo-including-its-submodules
如何在 git 子模块中“提交”更改? https://stackoverflow.com/questions/5542910/how-do-i-commit-changes-in-a-git-submodule
如何直接在当前目录中添加 git 子模块的内容,而不需要额外的项目文件夹? https://stackoverflow.com/questions/76462811/how-to-add-the-content-of-a-git-submodule-directly-in-the-curent-directory-with
git 子模块跟踪最新 https://stackoverflow.com/questions/9189575/git-submodule-tracking-latest?answertab=active#tab-top
B. 使用单独的存储库作为子树“public”
包括子树
-
删除主文件夹内的公用文件夹
-
提交主文件夹(=>应用公用文件夹的删除)
-
使用 subtree 命令在本地包含存储库的文件
git subtree add --prefix public https://github.com/user/new_repository.git main --squash
- 推送主文件夹(提交已由
subtree add
命令)
新鲜克隆
- 新克隆包括子树的所有文件。与子模块相比,不需要额外的命令或选项。
提交子树的更改
git subtree push --prefix public https://github.com/user/new_repository.git main --squash
可以引入一个alias对于 url,使命令的整体长度变短,使用git add remote
, see https://www.atlassian.com/git/tutorials/git-subtree https://www.atlassian.com/git/tutorials/git-subtree
拉取子树的变化
git subtree pull --prefix public https://github.com/user/new_repository.git main
子树相关文章
-
https://gist.github.com/SKempin/b7857a6ff6bddb05717cc17a44091202 https://gist.github.com/SKempin/b7857a6ff6bddb05717cc17a44091202
-
https://www.atlassian.com/git/tutorials/git-subtree https://www.atlassian.com/git/tutorials/git-subtree