Git子树拆分两个目录

问题描述 投票:8回答:2

我一直关注this excellent answer,将git存储库的子目录提取到其自己的存储库中,同时保留了完整的历史记录。

我的存储库看起来像:

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

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

如果我运行以下命令:

git subtree split -P src/math -b math

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

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

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

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

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

src/
    math/
tests/
    math/

即,保留原始目录结构,但丢弃命令行中未明确提及的所有内容。

我该怎么做?

git git-subtree
2个回答
3
投票

根据您的需要,可能会遇到git filter-branch

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

另请参见Rewriting Git History

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

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


0
投票

使用git-subtree add拆分

# First create two *split-out* branches
cd /repos/repo-to-split
git subtree split --prefix=src/math --branch=math-src
git subtree split --prefix=test/math --branch=math-test

# Now create the new repo
mkdir /repos/math
cd /repos/math
git init

# This approach has a gotcha:
# You must commit something so "revision history begins",
# or `git subtree add` will complain about.
# In this example, an empty `.gitignore` is commited.
touch .gitignore
git add .gitignore
git commit -m "add empty .gitignore to allow using git-subtree"

# Finally, *split-in* the two branches
git subtree add --prefix=src/math ../repo-to-split math-src
git subtree add --prefix=test/math ../repo-to-split math-test

[C0] 2.23.0]对我有用。还请注意,您可以在split-in时间设置不同的前缀,即,将git --version添加到src/math/,并将src/添加到test/math/

旁注

:在提交给远程用户之前,在新存储库中使用test/,以查看生成的历史记录是否对您足够。就我而言,我有一些带有重复消息的提交,因为我的回购记录太脏了,但是对我来说没关系。

git log

© www.soinside.com 2019 - 2024. All rights reserved.