如何将特定文件从一个git存储库复制到另一个,从而保留历史记录 [英] How to copy specific files from one git repo to another, preserving history

查看:215
本文介绍了如何将特定文件从一个git存储库复制到另一个,从而保留历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的私有存储库,其中包含我想放入GitHub的文件,但是我只想包含某些文件,或者仅包含具有某些文件扩展名的文件,以及它们的提交历史记录.

I have an old private repo with files I'd like to put in GitHub, however I'd like to only include certain files or perhaps only files with certain file extensions, along with their commit history.

但是我不想在提交历史记录中包括任何排除的文件.

However I don't want to include any of the excluded files in the commit history.

我想最好提供一个我要复制的此类文件列表的文件.

I guess it would be ideal to provide a file of list of such files I'd like to copy.

推荐答案

文件没有提交历史记录.提交 are 历史记录,并且提交具有文件.

Files don't have commit history. Commits are history, and commits have files.

您要么拥有这些提交(以及那些文件以及所有 other 文件),要么您没有这些提交(因此也没有那些文件).

You either have those commits (and thus those files plus all the other files), or you don't have those commits (and thus don't have those files).

如果您想要一个历史记录,其中仅存在 个文件,而没有任何其他文件属于保存这些文件的提交,则必须构造一个新的历史记录其中仅存在那些文件.

If you want a history in which only those files exist, without any of the other files that are part of the commits that hold those files, you must construct a new history in which only those files exist.

具体如何操作取决于您自己.您可以进行一次提交(没有其他历史记录-新的历史记录只是一次提交,并按原样保留文件). Mike Faber的答案,或多或少会得到您的帮助.或者,您可以使用git filter-branch BFG 来编辑存储库,生成一系列新的替换提交,这些提交包含要保留的文件,但要避免丢弃要丢弃的文件.然后,您将拥有新的伪造历史记录,其中仅提交了那些文件.

Exactly how you go about doing this is up to you. You can make one single commit (no additional history—the new history is just one commit, with the files as they are). That's more or less what you will get with Mike Faber's answer. Or, you can use git filter-branch or The BFG to edit a clone of the repository, producing a series of new replacement commits that have the files you want to keep, but avoid the files you want discarded. Then you'll have the new fabricated history in which only those files were committed.

请注意,所有这些操作均对原始存储库及其提交没有任何影响:您正在使用一个新的历史记录来构成一个全新的存储库,在新的历史记录中,只有那些文件才出现在提交中. 如何实现此结果并不重要.根据定义,这些文件而不是其他文件出现的历史记录是新的历史记录,因此属于新的不同的存储库.

Note that this all has no effect on the original repository and its commits: you're making what amounts to a whole new repository, with a new history in which only those files appear in the commits. It doesn't matter how you achieve this result. A history in which those files appear, but not the other files, is by definition a new and different history, and therefore belongs in a new and different repository.

(通常,仅使用文件的最新副本就足够了,在这种情况下,只需使用一种使一次提交的存储库保存文件的最新版本的方法即可.通常比编写奇特的过滤器-分支过滤器要容易得多. .我实际上并没有使用BFG;也许这也使这项工作变得容易.)

(Often, just the latest copy of the files suffices, in which case, just use a method that makes a repository with one commit, holding the latest versions of the files. That's generally easier than writing a fancy filter-branch filter. I have not actually used The BFG; perhaps that also makes this job easy.)

这篇关于如何将特定文件从一个git存储库复制到另一个,从而保留历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆