gpt4 book ai didi

git-过滤器分支 : leave directory structure unchanged

转载 作者:太空狗 更新时间:2023-10-29 13:21:13 26 4
gpt4 key购买 nike

我的存储库中有一个目录结构:

|-repository/
| | |-repository/fileOne
| |
| |-subOne/
| | |-subOne/fileTwo
| |
| |-subTwo/
| |-subTwo/fileThree

我想将目录 subOne 移动到不同的存储库中。为此,我使用 git-filter-branch 分离 subOne,如下所示:

$ git filter-branch --subdirectory-filter subOne --prune-empty

这给我留下了一个看起来像这样的分支历史:

$ git log --oneline
0c0ea11 subOne history
6318bba subOne history
c96fddb Initial commit

这正是我想要的;我可以从另一个存储库执行 git fetchgit merge,这会将 subOne 和历史记录添加到另一个存储库中。太好了。

但是,git-filter-branch 已经删除了 subOne。检查我分支上的目录结构:

$ ls
fileOne
$ pwd
/this/is/a/path/repository

pwd 应该读作:/this/is/a/path/repository,但是ls应该显示subOne 在存储库中。相反,我得到的是 subOne 中的文件向上移动,并且目录被删除。

如何在不丢失目录的情况下提取 subOne

最佳答案

pwd should read: /this/is/a/path/repository, but ls should show subOne inside the repository. What I've got instead is the files from subOne moved upwards, and the directory removed.

原来这是 git-filter-branch --subdirectory-filter默认操作。引用自 man git-filter-branch:

The result will contain that directory (and only that) as its project root.

要解决此问题,我想将 / 中的所有内容移动到 /subdirectory。幸运的是,man git-filter-branch 中的 EXAMPLES 下有一段看起来像巫术的代码:

To move the whole tree into a subdirectory, or remove it from there:
git filter-branch --index-filter \
'git ls-files -s | sed "s-\t\"*-&newsubdir/-" |
GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
git update-index --index-info &&
mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD

编辑:这在 MAC OS X 上不起作用。看我的问题here想要查询更多的信息。 BSD sedGNU sed 不同,这很烦人。要在 OS X 上运行该示例,请将 sed 更改为 gsed;然后过滤器工作得很好。

关于git-过滤器分支 : leave directory structure unchanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9519863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com