简体   繁体   English

从目录中的分支创建新的git repo

[英]Create a new git repo off of a branch in a directory

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? 在我的Git存储库中,我有一个目录A。在分支中,我在A下创建了一个目录B。有没有办法在目录B中创建/初始化一个新的Git存储库? Would copying that directory, clearing all of the .git data, and do a git init in that new copied directory do the trick? 将复制该目录,清除所有.git数据,并在新复制的目录中执行git init来解决问题吗?

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 . 我认为将整个仓库复制到一个新文件夹,然后使用git rm丢弃不需要的文件是一种更好的做法。

Note that you can split a git repo, making a subdirectory in its own repo with git filter-branch. 请注意,您可以拆分git repo,并使用git filter-branch在自己的repo中创建一个子目录。

See " How can I split a repository apart into smaller atomic repositories? " 请参阅“ 如何将存储库拆分为较小的原子存储库?
(and " Detach subdirectory into separate Git repository ": (以及“将子目录分离到单独的Git存储库中 ”:

To rewrite the repository to look as if foodir/ had been its project root, and discard all other history: 要重写存储库,使其看起来好像foodir/已经是其项目根,并丢弃所有其他历史记录:

   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 . 或者,您可以将该子仓库包含为子模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM