我在一个名为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-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递归子模块的依赖问题。

其他回答

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

A <-> yyy, B <-> XXX

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

$ git branch -d B-master

然后,推动更改。

这对我很管用。

我可以为你的问题建议另一个解决方案(替代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递归子模块的依赖问题。

可能最简单的方法是将XXX的东西拉到YYY的分支中,然后合并到master中:

多:

git remote add other /path/to/XXX
git fetch other
git checkout -b ZZZ other/master
mkdir ZZZ
git mv stuff ZZZ/stuff                      # repeat as necessary for each file/dir
git commit -m "Moved stuff to ZZZ"
git checkout master                
git merge ZZZ --allow-unrelated-histories   # should add ZZZ/ to master
git commit
git remote rm other
git branch -d ZZZ                           # to get rid of the extra branch before pushing
git push                                    # if you have a remote, that is

实际上,我刚刚用我的几个回购尝试了这个,它是有效的。不像Jörg的答案,它不会让你继续使用另一个回购,但我不认为你指定无论如何。

注意:由于这篇文章最初写于2009年,git添加了下面答案中提到的子树合并。我今天可能会用这个方法,当然这个方法仍然有效。

简单的方法是使用git format-patch。

假设我们有两个git存储库foo和bar。

foo包含:

福.txt 。去

栏包含:

酒吧.txt 。去

我们希望以foo结束,其中包含bar历史记录和这些文件:

foo . txt。 git。 雷丁酒吧。. txt。

要做到这一点:

 1. create a temporary directory eg PATH_YOU_WANT/patch-bar
 2. go in bar directory
 3. git format-patch --root HEAD --no-stat -o PATH_YOU_WANT/patch-bar --src-prefix=a/foobar/ --dst-prefix=b/foobar/
 4. go in foo directory
 5. git am PATH_YOU_WANT/patch-bar/*

如果我们想重写所有从bar提交的消息,我们可以这样做,例如在Linux上:

git filter-branch --msg-filter 'sed "1s/^/\[bar\] /"' COMMIT_SHA1_OF_THE_PARENT_OF_THE_FIRST_BAR_COMMIT..HEAD

这将在每条提交消息的开头添加“[bar]”。

添加另一个答案,因为我认为这有点简单。将repo_dest拉入到repo_to_import中,然后推入——set-upstream url:repo_dest master。

这种方法对我来说很有效,我把几个较小的回购导入一个较大的回购中。

如何将repo1_to_import导入到repo_dest

# checkout your repo1_to_import if you don't have it already 
git clone url:repo1_to_import repo1_to_import
cd repo1_to_import

# now. pull all of repo_dest
git pull url:repo_dest
ls 
git status # shows Your branch is ahead of 'origin/master' by xx commits.
# now push to repo_dest
git push --set-upstream url:repo_dest master

# repeat for other repositories you want to import

重命名或移动文件和dirs到原始回购所需的位置,然后再进行导入。如。

cd repo1_to_import
mkdir topDir
git add topDir
git mv this that and the other topDir/
git commit -m"move things into topDir in preparation for exporting into new repo"
# now do the pull and push to import

以下链接中描述的方法启发了这个答案。我喜欢它,因为它看起来更简单。但是要小心!有龙!https://help.github.com/articles/importing-an-external-git-repository git push——镜像url:repo_dest将本地的回购历史和状态推送到远程(url:repo_dest)。但是它会删除旧的历史记录和远程状态。乐趣随之而来!: - e