How to copy specific files from one git repo to another, preserving history

时光毁灭记忆、已成空白 提交于 2019-12-24 15:43:51

问题


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.


回答1:


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

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.

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.

(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.)




回答2:


Here's how you might approach this.

  1. Create a new folder for the new repository.
  2. Run a git init in the new folder to initialize it as a git repository.
  3. Run git clone -n [path to your old private git repo]. This will clone but not checkout any files. You'll be left with just the .git/ folder of the old private repo.
  4. Run git checkout [file you want] for each file you want. You can probably use wildcards here (you mentioned file extensions).
  5. Once you have all the files you want, delete the remote origin that was added by the clone command, and add a remote named origin with the path to your new repository's location.
  6. Push


来源:https://stackoverflow.com/questions/55522392/how-to-copy-specific-files-from-one-git-repo-to-another-preserving-history

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!