首页 > 解决方案 > 如何将文件夹从一个 Git 存储库共享到另一个?

问题描述

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

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

标签: gitgit-subtree

解决方案


1.子模块

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

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

2.子树

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

检查例如此页面

# 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

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


推荐阅读