我试图找到一个解决方案来管理跨多个存储库的文件单向同步的结构(在GitHub中)。
问题如下:
(boilerplates).
例如,考虑到这种结构:
REPO1
template-1
index.html
template-2
template-3REPO2
alice
index.html (from template-1)
bob
index.html (from template-2)
eve将以下代码视为index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<script src="index.js"></script>
</body>
</html>并且说爱丽丝复制了它并创建了她的index.html副本如下:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<script src="index.js"></script>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi non nulla lectus.</p>
</body>
</html>当我向原始index.hrml添加以下代码时
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">预期的解决方案应该允许Alice获得这个增量,而不覆盖她的文件中唯一的段落。
I've tried using submodules, adding several remotes, and using github actions to sync the relevant files, but none of those were able to sync specific files (only the entire repositories) or do so without overwriting the changes.发布于 2022-12-04 12:47:29
解决这个问题的一个潜在方法是使用Git的一个特性,称为“子树”。子树允许您将一个存储库的内容合并到另一个存储库作为子目录。这允许您将代码模板保存在单独的存储库中,并根据需要将它们合并到第二个存储库中,而无需覆盖可能进行的任何本地更改。
要使用子树,首先需要将模板存储库作为远程添加到第二个存储库中:
$ git remote add templates <url-of-templates-repo>接下来,您需要将模板存储库作为子树拖放到第二个存储库中。在本例中,我们将把模板拉到第二个存储库中的一个名为“样板”的目录中:
git subtree add --prefix=boilerplates templates master这将将模板存储库的内容拖到第二个存储库中的“样板”目录中。然后,您可以根据需要使用模板,而无需覆盖您可能进行的任何本地更改。
要更新第二个存储库中的模板,可以使用以下命令:
$ git subtree pull --prefix=boilerplates templates master这将在不覆盖任何本地更改的情况下将对模板存储库所做的任何更改拉到第二个存储库中。
您还可以使用子树将对第二个存储库中的模板所做的更改推送回模板存储库。例如,如果对第二个存储库中的"template-1“目录进行更改,可以使用以下命令将这些更改推回模板存储库:
$ git subtree push --prefix=boilerplates/template-1 templates master这将将您对第二个存储库中的"template-1“目录所做的更改推回模板存储库,而不会影响模板存储库中的任何其他文件。
总的来说,使用子树可以让您轻松地管理模板和第二个存储库之间的关系,同时仍然能够在不覆盖模板存储库的情况下进行本地更改。
https://stackoverflow.com/questions/74675683
复制相似问题