是否可以在另一个 git 存储库中拥有一个 git 存储库
我有一个使用 git 管理的 django 项目。我正在将它推送到我的主机。现在,我希望能够将其中一个目录(包括所有子目录)推送到另一个 git 存储库。如果有可能的话,怎么可能呢?
编辑:所以我希望该目录本身就是一个 git 存储库。
I have a django project managed with git. I am git pushing it to my host. Now, I want to be able to push just one of the directories (inc. all sub dirs) to another git repo. How is it possible, if it is possible at all?
Edit: So I want that directory to be a git repo itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以查看 git 对 子模块 的支持。您可以将第二个存储库作为子模块添加到主项目中,例如使用以下命令:
当您更改为
library-code
子目录时,就好像父存储库不存在 - 您可以更改origin
使用可以推送的传输,然后推送,就好像它完全独立一样。要指定您希望子模块处于特定版本,您应该更改为子模块并使用 git checkout 切换到正确的版本。然后,您更改回主存储库和阶段,并提交新的子模块版本:
主存储库的树仅存储子模块应位于的版本,因此当您推送主存储库时,它不会推送任何子模块中的文件。
为了拆分此子目录,同时保留历史记录,您需要克隆原始存储库和用户 git filter-branch 来重写历史记录,如本答案所述:
然后您可以将其推送到新创建的存储库 GitHub 存储库,返回到原始项目,删除子目录,并将其替换为如上所述的子模块。
如果您不太熟悉 git 概念,那么这对您来说可能会很困难 - 我建议您事先阅读 git 子模块。
You could look at git's support for submodules. You would add the second repository as a submodule to your main project, e.g. with commands like:
When you change into the
library-code
subdirectory, it's as if the parent repository doesn't exist - you can changeorigin
to use a transport that you can push over and then push as if it were completely independent.To specify that you want the submodule to be at a particular version, you should change into the submodule and use
git checkout
to switch to the right version. Then you change back up to the main repository and stage and commit that new submodule version with:The tree of the main repository just stores the version that the submodule should be at, so when you push the main repository, it's not pushing any of the files in the submodule.
In order to split off this subdirectory, while preserving history, you'll need to clone your original repository and user
git filter-branch
to rewrite the history, as described in this answer:Then you can push that to a newly created repository GitHub repository, return to your original project, remove the subdirectory, and replace it with the submodule as described above.
If you're not very familiar with git concepts then this may be difficult for you - I would recommend reading up on git submodules beforehand.
当您对特定目录进行更改、暂存这些更改并提交和推送时,您实际上只是在“推送”该目录。
如果其他 Git 存储库只有您想要推送的目录,并且这就是您希望将其本身作为存储库的原因,您可以查看 Git 子模块。
When you make changes to a particular directory, stage those changes and commit and push, you are effectively "pushing" just that directory.
If the other Git repo has only the directory you want to push, and that is why you want to have it as a repo in itself, you can look at Git Submodules.
你研究过 git 子模块吗?它允许 git 存储库干净地嵌套,但保持完全独立。
这意味着您可以拥有一个具有不同远程 url 的子目录用于推/拉和不同的提交树+日志,而不会弄乱主项目。
https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial
Have you looked into git submodules? It allows git repositories to cleanly nest, but remain fully independent.
This means you can have a subdirectory with a different remote url for push/pull and different commit tree+log without cluttering up the main project.
https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial