在 Git 中导出具有历史记录的子树

发布于 2024-08-09 12:13:32 字数 182 浏览 4 评论 0原文

我的 Git 存储库中有一个文件夹,我想将其移至其自己的存储库中。是否可以将该文件夹的历史记录与该文件夹一起移动?

我之前只是执行了 git rm -r --cached subfolder/ 操作,然后在子文件夹上执行 git init 操作。但是,历史记录不会导入到新存储库中。

I have a folder in my Git repository that I'd like to move out in to its own repository. 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. However, the history is not imported in to the new repository.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女七分熟 2024-08-16 12:13:32

引用 git-filter- 中的示例分支(1)

重写存储库,使其看起来好像 foodir/ 是其项目根目录,并丢弃所有其他历史记录:

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

因此,您可以将库子目录转变为自己的存储库。请注意 -- 将过滤器分支选项与修订选项分开,以及 --all 用于重写所有分支和标记。

Quoting an example from git-filter-branch(1)

To rewrite the repository to look as if foodir/ has been its project root, and discard all other history:

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

Thus you can, e.g., 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.

一紙繁鸢 2024-08-16 12:13:32

对于 2021 年(或之后)到达这里的其他人来说,核心 git 团队并不强烈推荐 git filter-branch。

警告
git filter-branch 有很多陷阱,可能会对预期的历史重写产生不明显的损坏(并且可能会让您几乎没有时间来调查此类问题,因为它的性能如此糟糕)。这些安全和性能问题无法向后兼容地修复,因此不建议使用它。请使用替代历史记录过滤工具,例如 git filter-repo。如果您仍然需要使用 git filter-branch,请仔细阅读安全(和性能)以了解 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"

For anyone else arriving here in 2021 (or later), git filter-branch is not strongly recommended by the core git team.

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). 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. 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.

Instead the recommended tool is git-filter-repo which makes easy work of this problem

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文