如何将git存储库转换为新存储库的子目录?

时间:2016-05-25 09:02:45

标签: git directory subdirectory

事实证明,我创建了一个git repo,它更适合作为更通用的存储库的子目录,比如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"

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

1 个答案:

答案 0 :(得分:0)

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非{} -zero退出代码被忽略。