我在一个名为XXX的文件夹中有一个Git存储库,还有一个名为YYY的Git存储库。

我想将XXX存储库作为名为ZZZ的子目录导入到YYY存储库中,并将所有XXX的更改历史添加到YYY中。

之前的文件夹结构:

├── XXX
│   ├── .git
│   └── (project files)
└── YYY
    ├── .git
    └── (project files)

文件夹结构后:

YYY
├── .git  <-- This now contains the change history from XXX
├──  ZZZ  <-- This was originally XXX
│    └── (project files)
└──  (project files)

这可以做到吗,或者我必须使用子模块?


当前回答

Git-subtree正是为这样的用例设计的脚本:将多个存储库合并为一个,同时保留历史(和/或分割子树的历史,尽管这似乎与这个问题无关)。从1.7.11版开始,它作为git树的一部分发布。

要合并一个<repo> at revision <rev> as subdirectory <prefix>的仓库,使用git子树add,如下所示:

git subtree add -P <prefix> <repo> <rev>

Git-subtree以一种更用户友好的方式实现子树合并策略。

对于您的情况,在存储库YYY中,您将运行:

git subtree add -P ZZZ /path/to/XXX.git master

缺点是在合并的历史中文件没有前缀(不在子目录中)。因此,git log ZZZ/a会显示除了合并历史之外的所有更改(如果有的话)。你可以:

git log --follow -- a

但这不会显示合并历史中其他的变化。

换句话说,如果不更改存储库XXX中的ZZZ文件,则需要指定——follow和一个无前缀路径。如果在两个存储库中都更改它们,则有两个命令,其中没有一个显示所有更改。

这里有更多。

其他回答

在我的例子中,我只想从另一个存储库(XXX)导入一些文件。子树对我来说太复杂了,其他的解都不行。这就是我所做的:

ALL_COMMITS=$(git log --reverse --pretty=format:%H -- ZZZ | tr '\n' ' ')

这将为您提供一个以空格分隔的列表,其中包括所有影响我想要导入的文件(ZZZ)的反向顺序(您可能还必须添加——follow以捕获重命名)。然后我进入目标存储库(YYY),将另一个存储库(XXX)添加为远程,从它中获取,最后:

git cherry-pick $ALL_COMMITS

这会将所有提交添加到分支,因此您将拥有所有具有历史记录的文件,并且可以对它们做任何您想做的事情,就像它们一直在这个存储库中一样。

我可以为你的问题建议另一个解决方案(替代git-submodules) - gil (git链接)工具

它允许描述和管理复杂的git存储库依赖关系。

同时也为git递归子模块依赖问题提供了解决方案。

假设你有以下项目依赖项: 示例git存储库依赖关系图

然后你可以用存储库关系描述定义.gitlinks文件:

# Projects
CppBenchmark CppBenchmark https://github.com/chronoxor/CppBenchmark.git master
CppCommon CppCommon https://github.com/chronoxor/CppCommon.git master
CppLogging CppLogging https://github.com/chronoxor/CppLogging.git master

# Modules
Catch2 modules/Catch2 https://github.com/catchorg/Catch2.git master
cpp-optparse modules/cpp-optparse https://github.com/weisslj/cpp-optparse.git master
fmt modules/fmt https://github.com/fmtlib/fmt.git master
HdrHistogram modules/HdrHistogram https://github.com/HdrHistogram/HdrHistogram_c.git master
zlib modules/zlib https://github.com/madler/zlib.git master

# Scripts
build scripts/build https://github.com/chronoxor/CppBuildScripts.git master
cmake scripts/cmake https://github.com/chronoxor/CppCMakeScripts.git master

每一行描述git链接的格式如下:

存储库的唯一名称 存储库的相对路径(从.gitlinks文件的路径开始) Git存储库,将用于Git克隆命令 要检出的存储库分支 空行或以#开头的行不会被解析(作为注释处理)。

最后,你必须更新你的根示例库:

# Clone and link all git links dependencies from .gitlinks file
gil clone
gil link

# The same result with a single command
gil update

因此,您将克隆所有必需的项目,并以适当的方式将它们相互链接。

如果你想提交一些存储库中的所有更改,以及子链接存储库中的所有更改,你可以用一个命令来完成:

gil commit -a -m "Some big update"

Pull、push命令的工作原理类似:

gil pull
gil push

Gil (git链接)工具支持以下命令:

usage: gil command arguments
Supported commands:
    help - show this help
    context - command will show the current git link context of the current directory
    clone - clone all repositories that are missed in the current context
    link - link all repositories that are missed in the current context
    update - clone and link in a single operation
    pull - pull all repositories in the current directory
    push - push all repositories in the current directory
    commit - commit all repositories in the current directory

更多关于git递归子模块的依赖问题。

没有足够的代表给x-yuri的回答加上评论,但它工作得很漂亮,保存了历史。 我正在与两个工作的本地回购工作,并收到这个错误:

中止:拒绝破坏性地覆盖此后的回购历史 这看起来不像一个新的克隆体。 (预计新包装的回购) 请改用一个新的克隆体做手术。如果你想继续,那就用武力。

与其担心——force标志的含义,我先在本地克隆了这个repo:

cd tempDir
git clone <location of repo to be merged> --no-local

用这个新克隆的拷贝来执行x-尤里布置的一系列命令。 最后,在:git filter-repo——to-subdirectory-filter a中,a是你要导入的repo根文件夹的名称。

Git-subtree正是为这样的用例设计的脚本:将多个存储库合并为一个,同时保留历史(和/或分割子树的历史,尽管这似乎与这个问题无关)。从1.7.11版开始,它作为git树的一部分发布。

要合并一个<repo> at revision <rev> as subdirectory <prefix>的仓库,使用git子树add,如下所示:

git subtree add -P <prefix> <repo> <rev>

Git-subtree以一种更用户友好的方式实现子树合并策略。

对于您的情况,在存储库YYY中,您将运行:

git subtree add -P ZZZ /path/to/XXX.git master

缺点是在合并的历史中文件没有前缀(不在子目录中)。因此,git log ZZZ/a会显示除了合并历史之外的所有更改(如果有的话)。你可以:

git log --follow -- a

但这不会显示合并历史中其他的变化。

换句话说,如果不更改存储库XXX中的ZZZ文件,则需要指定——follow和一个无前缀路径。如果在两个存储库中都更改它们,则有两个命令,其中没有一个显示所有更改。

这里有更多。

请参阅本文中的基本示例,并考虑在存储库上进行这样的映射:

A <-> yyy, B <-> XXX

完成本章描述的所有活动(合并后),移除分支B-master:

$ git branch -d B-master

然后,推动更改。

这对我很管用。