jam*_*san 94
引用一个例子 git-filter-branch(1)
要重写存储库,使其看起来像foodir /已成为其项目根目录,并丢弃所有其他历史记录:
git filter-branch --subdirectory-filter foodir -- --all
Run Code Online (Sandbox Code Playgroud)
因此,您可以将库子目录转换为自己的存储库.注意 - 将过滤器分支选项与修订选项分开,并且--all重写所有分支和标记.
对于 2021 年(或之后)来到这里的其他人,核心 git 团队不强烈推荐 git filter-branch。
警告 git filter-branch 有很多陷阱,这些陷阱可能会对预期的历史重写产生不明显的破坏(并且由于它的性能如此糟糕,因此您几乎没有时间调查这些问题)。这些安全和性能问题无法向后兼容修复,因此不推荐使用它。请使用替代的历史过滤工具,例如 git filter-repo。如果您仍然需要使用 git filter-branch,请仔细阅读 SAFETY (and PERFORMANCE) 以了解 filter-branch 的地雷,然后警惕地尽可能多地避免其中列出的危害。
相反,推荐的工具是git-filter-repo,它可以轻松解决此问题
git filter-repo --path subfolder/ # Filter the repo to just the sub-dir
git mv subfolder/* . # Move the contents of the sub-dir to the root
git commit -m "Filtered from main repo"
Run Code Online (Sandbox Code Playgroud)