git - 子目录到独立存储库中,然后 merge 回主存储库

标签 git merge subtree

我想从现有 git 存储库的子目录中创建独立的存储库,然后能够将其 merge 回主存储库。基本上我想为特定的工作分离一个单一的 git 存储库的一个子目录,并能够将提交 merge 回单一的 repo。

我尝试的是拆分一个子树:

% git subtree split -P project_a/directory_a -b directory_a_branch

然后我可以将 directory_a_branch 克隆到单独的存储库中并开始处理它:

% git clone -b directory_a_branch --single-branch file:///path/to/main_repo

稍后我可以将更改推送回主存储库(在 directory_a_branch 下)——现在一切正常,按部就类。

问题始于我 merge directory_a_branch 的策略,该策略失败了,因为我显然无法完全理解我在做什么。

我从 Pro Git 书中阅读了“子树 merge ”:https://git-scm.com/book/en/v1/Git-Tools-Subtree-Merging并尝试:

% git read-tree --prefix=project_a/directory_a -u directory_a_branch
error: Entry 'project_a/directory_a/README' overlaps with 'project_a/directory_a/README'.  Cannot bind.

这里讨论了类似的问题:git: merging a subtree from one branch to another ,但对我来说缺乏足够的答案。

创建子树分支不是我的意图,这个问题可以通过其他方式解决。也许有子模块?

我的意图是一个存储库就像另一个存储库的子存储库,并且将工作 merge 回主存储库会尽可能简单,因为两个存储库共享相同的文件。

有没有可能用 Git 解决这个任务而不用丑陋的技巧?

最佳答案

这篇文章描述了解决此问题(可能会满足特定用例)的一种方法:https://lostechies.com/johnteague/2014/04/04/using-git-subtrees-to-split-a-repository/

基本上,您从主分支(并提交)中删除 project_a/directory_a,然后从(远程)分支中删除 subtree add --prefix,如下所示:

git subtree add --prefix=project_a/directory_a remote branch

但这不是 100%,因为有更好的方法来解决这个问题。

关于git - 子目录到独立存储库中,然后 merge 回主存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30704352/

相关文章:

Git 标记语法 : why do some flags have -one dash and some have --two?

git - 如何使用 TortoiseGit 查看远程更改

Java 8 - 合并所有包含公共(public)元素的子集

r - 如何合并具有相同列名的数据框

neo4j - 在 Neo4j 中递归匹配子树

git - 如何使用 Subgit Hook SVN 更新

eclipse - 控制 eGit 对符号链接(symbolic link)的处理

减少 R 中函数的额外参数

git - 使用git但不使用git子模块在子目录中安装wordpress

git - 当我们使用子树 merge 策略时,git如何找到子树?