【问题标题】:git subtree from one branch to several locations of same project从一个分支到同一项目的多个位置的 git 子树
【发布时间】:2013-10-27 09:59:13
【问题描述】:

我有一个包含内容的 git 存储库 A:

 <shared files dir>/
        sharedFile1
        sharedFile2

我有另外的 git 存储库 B 和 2 个项目,每个项目都必须使用存储库 A 中的共享文件。因此文件结构将类似于:

Project_X
   <shared files dir>/
      sharedFile1
      sharedFile2
   <project X dirs>

Project_Y
   <shared files dir>/
      sharedFile1
      sharedFile2
   <project Y dirs>

我希望仅在存储库 A 中更新共享文件,并且我需要允许 project_X 和 Project_Y 轻松获取这些更新。

实际上,符号链接可能正是我需要的解决方案,但不幸的是,我使用的文件系统不支持它。

Git 子树看起来也是合适的解决方案,但问题是我找不到如何将同一分支的子树合并到同一存储库中的 2 个位置。我成功创建了我需要的文件结构:

git read-tree --prefix=Project_X/shared_files_dir/ -u shared_remote_branch
git read-tree --prefix=Project_Y/shared_files_dir/ -u shared_remote_branch

创建所需的文件结构(正如我在问题顶部所描述的那样),但尝试合并更新只会将更改带到我创建的最后一个子树:

git merge --squash -s subtree --no-commit shared_remote_branch

仅使用来自 shared_remote_branch 的更改更新 Project_Y/shared_files_dir/,而不是 Project_X/shared_files_dir/。

我会很感激任何想法。关于如何将子树从一个分支合并到 2 个位置 (Project_X/shared_files_dir/) 和 (Project_Y/shared_files_dir/) 或如何在任何其他方法中进行合并

【问题讨论】:

    标签: git share git-submodules git-subtree


    【解决方案1】:

    从git1.8.2开始,你可以注册一个submodulefollowing the latest commits of a given branch

    所以我会推荐这种方法而不是子树,因为它允许:

    • 为 shared_files 存储库维护单独的历史记录
    • 轻松更新到分支的最后一次提交

      git submodule update --remote
      

    你甚至可以convert an existing submodule in order to have it trakc the latest of a branch


    OP 选择了simpler solution

    我决定使用一种解决方法:

    • 在单独的仓库中管理共享模块和
    • 在我的主仓库中添加一个post-checkout 挂钩,它将那些共享文件从共享仓库复制到主仓库。

    我们是一个小团队,所以这种方法对我们来说是最简单的

    【讨论】:

    • 感谢您的回答。我不想使用子模块,因为在网络上对它们有很多担忧(例如,在进行 git submodule update 时丢失了你的更改)。所以我想知道是否可能有其他解决方案....
    • @user2924714 我明白,但我已经使用子模块几个月了(如在我的项目中:github.com/VonC/compileEverything/blob/master/…,其中子模块 Semantic-UI 遵循master),以及最新版本的 git ,所有这些问题都已得到解决或得到严重缓解。
    • 尝试使用子模块,我仍然看到它是一个开销,并且有太多的错误选项。你知道是否可以使用子树来完成它? (请参阅原始问题:我未能将同一分支的子树合并到同一存储库中的 2 个位置。我成功创建了我需要的文件结构: git read-tree --prefix=Project_X/shared_files_dir/ -u shared_remote_branch git read-tree --prefix=Project_Y/shared_files_dir/ -u shared_remote_branch 但是:git merge --squash -s subtree --no-commit shared_remote_branch 仅更新 Project_Y/shared_files_dir,而不是同时更新
    • @user2924714 实际上我没有:子树真的不是我会选择的方法,而且我从来没有遇到子模块的“错误”。
    • 感谢您的回答和建议。同时,我不会再研究子树和子模块。我决定使用一种解决方法——在单独的 repo 中管理共享模块,并在我的主 repo 中添加一个 post-checkout 挂钩,它将这些共享文件从共享 repo 复制到主 repo。我们是一个小团队,所以这种方法对我们来说是最简单的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多