简体   繁体   English

使用历史导出 Git 中的子树

[英]Export subtree in Git with history

I have a folder in my Git repository that I'd like to move out in to its own repository.我的 Git 存储库中有一个文件夹,我想将其移到自己的存储库中。 Is it possible to move the history of that folder along with the folder?是否可以随文件夹移动该文件夹的历史记录?

I've previously been doing just a git rm -r --cached subfolder/ and then git init on the subfolder.我以前一直在做一个git rm -r --cached subfolder/然后在子文件夹上执行git init However, the history is not imported in to the new repository.但是,历史不会导入到新存储库中。

Quoting an example from git-filter-branch(1)引用git-filter-branch(1)一个例子

To rewrite the repository to look as if foodir/ has been its project root, and discard all other history:重写存储库以使其看起来好像 foodir/ 一直是它的项目根目录,并丢弃所有其他历史记录:

git filter-branch --subdirectory-filter foodir -- --all

Thus you can, eg, turn a library subdirectory into a repository of its own.例如,您可以将库子目录转换为它自己的存储库。 Note the -- that separates filter-branch options from revision options, and the --all to rewrite all branches and tags.请注意将过滤器分支选项与修订选项分开的 --,以及用于重写所有分支和标签的 --all。

For anyone else arriving here in 2021 (or later), git filter-branch is not strongly recommended by the core git team.对于 2021 年(或之后)来到这里的其他人,核心 git 团队不强烈推荐 git filter-branch。

WARNING git filter-branch has a plethora of pitfalls that can produce non-obvious manglings of the intended history rewrite (and can leave you with little time to investigate such problems since it has such abysmal performance).警告 git filter-branch 有很多陷阱,可能会对预期的历史重写产生不明显的破坏(并且由于它的性能如此糟糕,因此您几乎没有时间调查这些问题)。 These safety and performance issues cannot be backward compatibly fixed and as such, its use is not recommended.这些安全和性能问题无法向后兼容修复,因此不推荐使用它。 Please use an alternative history filtering tool such as git filter-repo.请使用替代的历史过滤工具,例如 git filter-repo。 If you still need to use git filter-branch, please carefully read SAFETY (and PERFORMANCE) to learn about the land mines of filter-branch, and then vigilantly avoid as many of the hazards listed there as reasonably possible.如果您仍然需要使用 git filter-branch,请仔细阅读 SAFETY (and PERFORMANCE) 以了解 filter-branch 的地雷,然后警惕地尽可能多地避免其中列出的危害。

Instead the recommended tool is git-filter-repo which makes easy work of this problem相反,推荐的工具是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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM