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

nee*_*eel 3 git github

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

  1. 首先,我根据http://gbayer.com/development/moving-files-from-one-git-repository-to-another-preserving-history/ 中的建议尝试了git filter-branch --subdirectory-filter 结果:历史被维护,但只能在运行时查看 git log --follow 另外,在 Github 上无法看到原始提交历史。它只是将我的合并提交显示为该文件的唯一提交,并且不显示任何以前的提交。我仍然可以忍受这个限制并接受它作为解决方案。但我对这种方法的另一个担忧是,对于我想要复制的每个文件夹和每个文件,我需要多次克隆原始存储库,并且每次都重复所有 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选项/sf/answers/799838301/ 结果:这通常会在应用补丁时遇到合并错误。我尝试了使用-m --first-parent这个答案的建议之一。这解决了错误,但没有将任何合并扩展到他们的提交中,只是将合并列为单个提交。因此大部分提交历史都丢失了。所以我添加了--3way 的另一个选项。这一遍又一遍地通过提交,并没有导致任何可接受的解决方案。

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

谢谢。

nee*_*eel 6

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

首先使用名称在 Github 上创建新的空仓库 new-repo1

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
Run Code Online (Sandbox Code Playgroud)

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


归档时间:

查看次数:

8994 次

最近记录:

8 年,6 月 前