如何将文件夹从一个 Git 存储库共享到另一个存储库?

How do I share a folder from one Git repository to another?

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

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

1。 Submodule

使用 Git 的 子模块 ,您只能 link 将整个存储库放在另一个模块中。因此,一种方法是将整个 /sql 目录分离到一个单独的存储库中,然后 link 它作为一个子模块进入两个存储库。

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

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

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

Git 中没有任何本机功能可以满足您的需求。我想到了两个"tricks":

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

  2. 您可以 只签出 SQL 个文件。但是,这仍然会获取子模块的全部历史记录,只是在您的工作树中不会检出除 SQL 文件之外的其他文件。