我可以使用 Git 子模块将一个分支的副本保存在同一存储库中另一个分支的目录中吗?

Can I use Git submodules to keep a copy of one branch inside a directory of another branch in the same repository?

我正在编写 Web 开发教程。在其中,学生从名为 'chapter-1-start' 的 git 分支开始,当她完成该教程章节中的步骤时,她的代码应该看起来与 'chapter-1-complete.' 中的代码基本相同我会喜欢将 'chapter-1-complete' 包含在 'lib/reference_app' 等目录中的 'chapter-1-start' 分支中,这样她就可以比较或在必要时从完整的文件开始剪切和粘贴文件。这是我的目标:

  1. 将所有代码保存在一个存储库中。
  2. 'chapter-1-start' 内的参考分支 'chapter-1-complete' 应该是最新的 -- 不必更新。

我认为这对于子模块是可行的,但没有使用它们的经验。我担心通过递归引用丢失我的整个 git 历史记录。

为特定分支添加相同的 repo 作为子模块是我过去做过的一个技巧:参见“Copying Doxygen Documentation from gh-pages branch into a subfolder of Master branch”作为示例。

对于您的情况,学生可以在 his/her 自己的存储库中执行:

git branch chapter-1-complete origin/chapter-1-complete
git checkout chapter-1-start
git submodule add -b chapter-1-complete -- /remote/url/of/your/own/repo
git commit -m "ADd chapter-1-complete branch as submodule"
git push

然后在 chapter-1-start 中完成一个简单的 git submodule update --remote 就足以更新 chapter-1-complete 子文件夹(子模块的根目录)的内容。参见“git submodule follows branch”。