【问题标题】:How do I share a folder from one Git repository to another?如何将文件夹从一个 Git 存储库共享到另一个?
【发布时间】:2019-10-18 20:32:29
【问题描述】:

我有两个项目 [git repos] 共享一个数据库模式,但只有一个项目实际上包含 DDL SQL 文件。我知道我可以将包含 SQL 的那个添加为子树,但这将接管所有代码和所有内容 - 我需要的只是包含 SQL 文件的目录,因此我可以在第二个项目中创建架构以使用 H2 进行测试.我真的不想尝试让它们手动同步[从不工作] - 所以我希望简单地将项目 1 中的 /sql 文件夹链接到项目 2。

我也无法在 Git 中创建任何新的存储库。

【问题讨论】:

  • this question。您需要将要共享的文件提取到两个现有存储库都依赖的第三个存储库。

标签: git git-subtree


【解决方案1】:

1。 Submodule

使用 Git 的 子模块,您只能将整个存储库链接到另一个存储库中。因此,一种方法是将整个 /sql 目录分离到一个单独的存储库中,并将其作为子模块链接到两个存储库中。

在这种情况下,对链接存储库文件的更改将被推送到源存储库。

2。 Subtree

但也有一个 子树 可以满足您的需要。但是我从来没有用过,所以你必须尝试一下。

检查例如this page:

# Clone the target repo
git clone git@github.com:jclouds/jclouds.git
cd jclouds

# Add the source repository as a remote, and perform the initial fetch.
git remote add -f sourcerepo git@github.com:jclouds/jclouds-labs-openstack.git

# Create a branch based on the source repositories' branch that contains the state you want to copy.
git checkout -b staging-branch sourcerepo/master

# Here's where the two approaches diverge.
# Create a synthetic branch using the commits in `/openstack-glance/` from `sourcerepo`
git subtree split -P openstack-glance -b openstack-glance

# Checkout `master` and add the new `openstack-glance` branch into `/apis/openstack-glance/`. At this point, the desired result will have been achieved.
git checkout master
git subtree add -P apis/openstack-glance openstack-glance

# Clean up by removing the commits from the `openstack-glance` branch and the `sourcerepo` remote.
git branch -D openstack-glance staging-branch
git remote rm sourcerepo

在这种情况下,对链接子树或目录的更改不会被推送回源存储库,但我猜你需要的应该没问题。

【讨论】:

  • 这似乎与我在问题中提到的相同 - 这会将整个项目复制到另一个项目中,而不仅仅是包含 SQL 文件的文件夹。
  • 我不知道你指的是哪种方法,如果第一种,是的,你是对的,我在第一句话中就提到了。如果是第二个,应该可以根据我链接的那篇文章只链接子目录,但正如我所提到的,我没有任何经验。
  • 使用此方法是否可以通过拉取使该目录与源存储库保持最新?还是每次都要重复整个过程?
  • @Gandalf 哦,对不起,我真的不知道。你必须调查它。那么你选择了子树方法吗?但如果你能在这里分享结果,我会很高兴。我会更新我的答案。
【解决方案2】:

Git 中没有原生功能可以满足您的需求。我想到的两个“技巧”:

  1. 正如您所说,您不能创建第三个存储库来仅存储共享 SQL 文件,您可以在已包含 SQL 文件的存储库中创建一个 orphan branch,将文件移到那里,然后使用它作为两个 repos 中的子模块分支。

  2. 您可以combine the submodule approach with a sparse checkout 只签出 SQL 文件。但是,这仍然会获取子模块的整个历史记录,只是在您的工作树中不会检出除 SQL 文件之外的其他文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2011-03-21
    • 2010-10-30
    • 2023-04-06
    • 2012-01-09
    相关资源
    最近更新 更多