如何将 git 存储库变成新存储库的子目录?

How to turn a git repository into a subdirectory of a new repo?

事实证明,我创建的 git 存储库更适合作为更通用存储库的子目录,例如 module x。我怎样才能复制原始历史记录(没有签名的提交),这样现在所有路径都有前缀 module x\?

我当然可以简单地将所有内容移动到该子目录中作为一个新的提交,但我更喜欢一种解决方案,使历史看起来好像以前的 repo 总是在子目录中。


我尝试 git subtree add --prefix="module x" ../module_x master 从一个带有新存储库的临时并行文件夹(否则 git subtree 抱怨已经存在的前缀),但最终得到

fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Working tree has modifications.  Cannot add.

这似乎是由于缺少初始提交,因为如果我

touch .gitignore
git add .gitignore
git commit -m"Init"

它有效,但我最终在一个单独的分支中得到原始历史加上移动提交,所以只是我之前提到的手动解决方案...

git filter-branch --tree-filter 'mkdir -p module_x; mv all the files and* module_x/' HEAD

成功了。我正在使用 msysgit,但我想 Linux shopt -s extglob; mv !(module_x) module_x/ 应该工作得更好...

如果明确提及的文件在整个历史记录中都不存在,您可能需要耍点小花招,将 ; true 附加到 --tree-filter,这样 mv 的非零退出代码被忽略。