gpt4 book ai didi

git - 将多个文件和目录从一个 git repo 复制到另一个,同时保留其原始历史记录

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

我的要求:将一个 git 仓库分解成多个 git 仓库,保留与原始仓库相同的目录结构,并保留复制到新仓库的文件的提交历史记录。我已经尝试过的:

  1. 首先,我根据 http://gbayer.com/development/moving-files-from-one-git-repository-to-another-preserving-history/ 中的建议尝试了 git filter-branch --subdirectory-filter结果:历史保留,但只能在运行时查看git 日志 --follow另外,在 Github 上看不到原始的提交历史。它只是将我的 merge 提交显示为该文件的唯一提交,并且不显示任何以前的提交。我仍然可以忍受这个限制并接受它作为解决方案。但我对这种方法的另一个担忧是,对于我想要复制的每个文件夹和每个文件,我需要多次克隆原始存储库,并且每次都重复所有这 12 或 13 个步骤。我想知道是否有更简单的方法,因为我要移动很多文件。此外,由于该帖子已有 5 年历史,只是想知道是否有更新更简单的解决方案可用? (令人惊讶的是,谷歌大多将此博客显示为第一个搜索结果)

  2. 接下来我尝试对 Greg Bayer 之前的帖子发表评论 http://gbayer.com/development/moving-files-from-one-git-repository-to-another-preserving-history/#comment-2685894846此解决方案通过使用 git subtree split 使事情变得更简单,但结果与第一种情况中列出的结果相同。

  3. 然后我根据这个答案尝试了 git log --patch-with-statgit am 选项 https://stackoverflow.com/a/11426261/5497551结果:这通常会在应用补丁时遇到 merge 时出错。我尝试了使用 -m --first-parent 这个答案的建议之一这解决了错误但没有将任何 merge 扩展到他们的提交中,只是将 merge 列为单个提交。因此,大部分提交历史都丢失了。所以我又加了一个选项--3way。这通过提交反复进行,并没有导致任何可接受的解决方案。

总而言之,我更愿意使用第三种解决方案,如果只有一个选项可以让 merge 中的所有提交都列在新存储库的历史记录中的话。否则我必须坚持第一个解决方案,这在我的情况下有点不方便和乏味。任何建议,帮助将不胜感激。

谢谢。

最佳答案

这是对我有用的方法(结合@AD7six 和@Olivier 的回答)将我的 orig-repo 拆分成多个新的 repos。我在此处列出了仅创建一个新存储库 new-repo1 的步骤。但同样也被用来创建其他人。

首先在 Github 上创建一个名为 new-repo1

的新空 repo
git clone [Github url of orig-repo]

git clone --no-hardlinks orig-repo new-repo1
cd new-repo1
git remote rm origin
git checkout -b master //This step can be skipped. I had to do it since the default branch on my orig-repo was `develop`, but on the new-repo1 I wanted to create it as `master`

//I used a script here to delete files and directories not required in the new-repo1.
//But if you have very few files/dirs to be deleted then you can do the below.
git rm <path of file 1 to be deleted>
git rm <path of file 2 to be deleted>
git rm -rf <path of dir 1 to be deleted>

git commit -m "Deleted non-new-repo1 code"

git ls-files > keep-these.txt
git filter-branch --force --index-filter "git rm --ignore-unmatch --cached -qr . ; cat $PWD/keep-these.txt | xargs git reset -q \$GIT_COMMIT --" --prune-empty --tag-name-filter cat -- --all

rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now

git init
git remote add origin [Github url of new-repo1]
git push -u origin master

在此之后,我可以在 Github 上的 new-repo1 中查看文件的历史记录,也可以通过命令行使用 git log

关于git - 将多个文件和目录从一个 git repo 复制到另一个,同时保留其原始历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38182416/

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