如何删除git嵌套repo但保留其中一个分支的文件?

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

我刚刚将一个目录复制到另一个本地git仓库。事实证明,我复制的目录也是另一个git repo,所以现在我假设我称之为嵌套repo。

我想保留外部仓库,然后将内部仓库整合为外部仓库(根据需要修改远程原产地)。我担心的一个问题是因为内部仓库有几个分支。我想保留其中只有一个分支。

我对解决这个问题的最佳做法感到困惑。希望有人可以给我一些指示。谢谢!

git github gitlab
2个回答
1
投票

您只能保留文件,而不是历史记录。

  • 在另一个位置结帐所需的分支。
  • 将其他仓库复制到目标仓库中。
  • 从目标文件夹中删除.git文件夹和任何.git*文件。
  • 将新文件夹签入目标仓库

git shell这样做:

/other/location/otherPrepo>git checkout the-branch-wanted
/other/location/otherPrepo>cp -R  * -R /target/location/targetRepo/targetFolder/
/other/location/otherPrepo>rm -rf /target/location/targetRepo/targetFolder/.git*
/other/location/otherPrepo>cd  /target/location/targetRepo/
/target/location/targetRepo/>git add  targetFolder
/target/location/targetRepo/>git commit -m "added files from otherPrepo"

[编辑:]不要忘记签到...


0
投票

您可以使用git subtree从第二个存储库导入文件,同时保留其历史记录。

  1. 从干净的工作树开始(查看git status)。如果您已尝试手动复制文件,请将其删除。 Git会为你复制它们。
  2. cd到您的存储库的顶层。
  3. 运行git subtree add --prefix=inner path/to/other/repo some-branch。这将创建一个inner子目录,并将其他repo的some-branch的内容放入其中。
  4. 观察git log显示导入文件的历史记录。
© www.soinside.com 2019 - 2024. All rights reserved.