Create a new git repo off of a branch in a directory

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

In my Git repo, I have a directory A. In a branch, I've created a directory B under A. Is there a way to create/initialize a new Git repo in directory B? Would copying that directory, clearing all of the .git data, and do a git init in that new copied directory do the trick?

回答1:

Probably, but you'd lose all of your file history. This isn't a great approach. I'd think it's a better practice to clone the whole repo to a new folder, then discard the files you don't want using git rm.



回答2:

Note that you can split a git repo, making a subdirectory in its own repo with git filter-branch.

See "How can I split a repository apart into smaller atomic repositories?"
(and "Detach subdirectory into separate Git repository":

To rewrite the repository to look as if foodir/ had been its project root, and discard all other history:

   git filter-branch --subdirectory-filter foodir -- --all 

That would allow you keep the history, but will rewrite the one of your original repo (meaning other contributors would have to reset their clone to your new history).

Once done, you can clone that new repo right where your subdirectory were: nested repos are ignored by the parent repo.
Or you can include that sub-repo as a submodule.



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