How do I create a new git repository from a folder in an existing git repository?

牧云@^-^@ 提交于 2019-12-13 12:06:02

问题


I want to create a new git repository of a folder under an existing git repository. I want the history to be preserved in the new repository. How do I create this new git repository?


回答1:


You can clone it, and then apply on the clone a filter-branch in order to split that repository, extracting only the directory you want (with its full history).

See Detach subdirectory into separate Git repository

Note the git clone --no-hardlinks /XYZ /ABC used during the cloning step:

The --no-hardlinks switch makes git use real file copies instead of hardlinking when cloning a local repository.




回答2:


Assuming you only want to keep the subdirectory as a separate project: you can use git filter-branch for that.

  1. Create a new branch
  2. Define a "tree filter" that rearranges a checked out copy the way you want, ideally as a separate script
  3. Run git filter-branch --tree-filter=/path/to/script thebranch

You now have a separate branch that is not related to the normal development stream, containing just the subdirectory. Whoever clones the new branch gets only the subproject history.




回答3:


You can clone it and keep the files you want; history will then be preserved.



来源:https://stackoverflow.com/questions/5421386/how-do-i-create-a-new-git-repository-from-a-folder-in-an-existing-git-repository

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