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

标签 git git-subtree

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

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

最佳答案

1。 Submodule

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

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

2。 Subtree

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

检查例如 this page :

# Clone the target repo
git clone <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8afa1bc88afa1bca0bdaae6aba7a5" rel="noreferrer noopener nofollow">[email protected]</a>:jclouds/jclouds.git
cd jclouds

# Add the source repository as a remote, and perform the initial fetch.
git remote add -f sourcerepo <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="17707e6357707e637f62753974787a" rel="noreferrer noopener nofollow">[email protected]</a>: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

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

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

相关文章:

Git:访问同事的分支

git add 添加被忽略的文件

git - 如何列出根上的 git 子树?

git - git-subtree 添加后如何 rebase ?

git 子树错误 "fatal: refusing to merge unrelated histories"

git - commit time 在未来的后果

git - git log --decorate : (HEAD -> master) vs (HEAD, master 的输出差异)

git - 克隆的 github 存储库上的文件和目录权限

git - Git 中的部分子树上游推送

git - 如何减少推送子树的时间不断增加?