git - 如何跨文件夹或存储库共享文件

标签 git version-control workflow

我正在编写一个必须跨多个主题工作的代码片段。

假设我有三个主题:

  • 橙色
  • 绿色
  • 蓝色

每个主题里面都有一个名为“snippets”的文件夹,我把我的代码片段放在那里。

  • Orange/snippets/code.html
  • Green/snippets/code.html
  • Blue/snippets/code.html

“code.html”文件在每个主题中都完全相同。我在它自己的 GitHub 存储库中跟踪它,然后复制并粘贴到每个主题存储库。

我怎样才能在一个地方编辑这个片段并确保它在所有主题中得到更新?我正在寻找一种可扩展的方法,只要主题数量可以增长到 20-30。

在我的情况下,我有更多文件,然后是带有“code.html”的示例 - 它们位于两个文件夹中,总数约为 10 个。

我正在阅读有关 Git 子模块的内容,但我不确定它们是否适合我的问题。我不确定这个问题是否与 Git 完全相关,对此深感抱歉。

附言我正在开发 SaaS - 准确地说是 Shopify。所以任何类型的 PHP 技巧在这种情况下都不起作用。我需要以某种方式在本地处理文件。

编辑:现实生活中的例子:

我有这 4 个文件:

{{ theme }}/snippets/file1.liquid
{{ theme }}/snippets/file2.liquid
{{ theme }}/assets/file3.liquid
{{ theme }}/assets/file4.liquid

我无法创建子目录或其他目录。

最佳答案

假设您在存储库中有通用代码。 您可以在 <Theme>/snippets/common 中创建一个子模块.每次你想要构建/部署你的主题时,让它更新到最新的母版。

这意味着您的 code.html将在<Theme>/snippets/common/code.html

cd <Theme>/snippets

# To add the submodule 
git submodule add <common code repository> common
# 2 new files will be added to the repository
# .gitmodules and the "common" folder

# When you clone the repo in another machine
# Always do the following command 
# to get the files from the common repository
git submodule update --init

# If you made a change in the common repository
git submodule update --remote

关于git - 如何跨文件夹或存储库共享文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53327500/

相关文章:

git - 与推送 sourcetree 相关的大块头意味着什么

git - 如何查找给定提交是否在分支的第一父链上

git - 如何为多个提交更改 'git stash'?

git - 如何在源代码树 UI 中过滤当前分支和母版的历史记录?

java - Drools Flow 能否扩展到每天执行数十万个流程实例?

git - 我怎样才能一次删除我所有的 Git 存储?

TFS 中的文件未显示在解决方案资源管理器中

git - 使用 git 推送和 pull 我的 conda 环境

css - Compass 与 Gulp 混合

javascript - 在 CRM 2011 中将两个字段合并为一