gpt4 book ai didi

git - 从现有仓库的子目录创建新的 git 仓库

转载 作者:IT王子 更新时间:2023-10-29 00:55:31 26 4
gpt4 key购买 nike

我想从一个已经存在的存储库的子文件夹中创建一个单独的存储库。

Detach (move) subdirectory into separate Git repository恰好表明了这一点。但是,我无法用它获得干净的 repo 协议(protocol)。我最终遇到了新 repo 协议(protocol)的两个问题:

  1. 历史似乎是重复的;
  2. 我无法保留分支历史记录。

这是我做的:

$ git clone ssh://.../repo.git
$ cd repo
$ git filter-branch --subdirectory-filter subdirectory HEAD -- --all
$ git reset --hard
$ git gc --aggressive
$ git prune

在此之后,我似乎有了 repo 的原始历史和新历史。我输入“git log --all --graph”(或 gitk --all),我看到,作为第一次提交,第一个 repo 的初始提交。然后该图显示原始 repo 的完整历史记录,直到最后一次提交。然后,我在第一个历史记录之上有第二个历史记录,仅显示我想要的子文件夹的历史记录。但是在历史的那一部分我只有“主人”而没有分支/merge 。

“git log”、gitk 或 gitg 都只显示“flatten”历史:它们不显示子文件夹的 flatten 历史之前的初始 repo 历史。

我尝试只使用“filter-branch”命令,克隆生成的 repo(使用 --no-hardlinks),使用:

$ git filter-branch --subdirectory-filter subdirectory -- --all

代替:

$ git filter-branch --subdirectory-filter subdirectory HEAD -- --all

但结果是一样的。

是我做错了什么还是 git 坏了?我真的没主意了……使用 git 1.7.6。谢谢。

编辑:我认为问题可能来自 merge 提交被过滤器分支忽略的事实,因此给出了没有分支或 merge 的平坦历史......

最佳答案

这里有两个问题:

(1) 正如 Kevin Ballard 指出的那样,您需要删除 .git 目录中的 refs/original 目录以去除虚假的日志条目; IIRC 在您提到的问题中提到了这一点。

(2) 您必须一次转换一个分支。据我所知,这在任何地方都没有提到,但根据经验很容易发现。执行此操作的脚本如下所示:

for branch in $(git for-each-ref --format='%(refname:short)' refs/remotes/origin | grep -v HEAD); do
git checkout -b $(basename $branch) $branch
git filter-branch -f --subdirectory-filter subdirectory HEAD -- --all
done

请注意,您需要 -f 或类似 --original $(basename $branch)-original 的东西来强制 git 重用或重命名存储原始引用的 namespace 。另请注意,如果特定分支上不存在子目录,您可能会看到类似“找不到任何内容可重写”的消息——您可能希望从新存储库中删除这些分支。 (或者在运行脚本之前删除它们。)

关于git - 从现有仓库的子目录创建新的 git 仓库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6680709/

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