Git子树拆分两个目录 [英] Git subtree split two directories

查看:108
本文介绍了Git子树拆分两个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在遵循这个出色的答案,将git存储库的子目录提取到其自己的存储库中,同时保留了完整的历史记录.

I've been following this excellent answer to extract a subdirectory of my git repository into its own repository, while retaining the complete history.

我的存储库如下:

src/
    http/
    math/
tests/
    http/
    math/

我想创建一个仅包含src/mathtests/math目录的新分支.

I want to create a new branch that only contains the src/math and tests/math directories.

如果我运行以下命令:

git subtree split -P src/math -b math

它将创建一个包含src/math目录内容的分支,但会丢弃src/math/前缀.

It creates a branch that contains the contents of the src/math directory, but discards the src/math/ prefix.

如果我在两个目录中尝试相同的命令:

If I try the same command with two directories:

git subtree split -P src/math -P tests/math -b math

它仅提取tests/math的内容,忽略src/math,并丢弃tests/math前缀.

It only extracts the contents of tests/math, ignoring src/math, and also discarding the tests/math prefix.

总而言之,我希望最终的存储库看起来像这样:

To summarize, I would like my final repository to look like:

src/
    math/
tests/
    math/

也就是说,保留原始目录结构,但丢弃命令行中未明确提及的所有内容.

That is, keeping the original directory structure but discarding everything that's not explicitly mentioned in the command-line.

我该怎么做?

推荐答案

根据您的需求,您可能会放弃git filter-branch.

Depending on your needs you might get away with git filter-branch.

我不确定您要实现的目标,但是如果您只想删除一个已删除两个目录的存储库(在历史记录中?),这可能是您的最佳选择.

I'm not entirely sure what you are trying to achieve, but if you merely want to have a repository with two directories removed (in the history?) this is probably your best shot.

另请参见重写Git历史记录.

$ git filter-branch --tree-filter 'rm -rf tests/http src/http' --prune-empty HEAD

这将调查每个提交并从该提交中删除两个目录. 请注意,这会重写历史记录(即:更改您的提交sha),如果您与其他存储库有共同的历史记录,则会引起头痛.

This will look into each commit and remove the two directories from this commit. Be aware that this rewrites history (i.e.: alters your commit sha) and will cause headaches if you have a common history with another repository.

这篇关于Git子树拆分两个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆