简体   繁体   English

我可以使用子模块对git存储库进行“深层复制”吗?

[英]Can I make a “deep copy” of a git repository with submodules?

It is easy to clone an entire project plus all its submodules: 克隆整个项目及其所有子模块很容易:

git clone --recursive git@github.com:homer/powerplant.git

However, how do I create a clone that has all these submodules replaced by the HEADs of the individual sub-repositories themselves? 但是,如何创建一个克隆,将所有这些子模块替换为各个子存储库本身的HEAD? Preferably in such a way that this can be repeated easy, as soon as something changes. 优选地,以这样的方式使得一旦事情改变,这可以容易地重复。 The target should be a read-only "flat" version of the same overall code. 目标应该是相同总体代码的只读“平面”版本。 No merging should be necessary. 无需合并。

If this is possible, it would solve my Launchpad problems where bzr can import only repositories without submodules. 如果可以,它将解决我的Launchpad问题,其中bzr只能导入存储库而没有子模块。

If you want to start with submodules' files but not as git repos, simply do the following: 如果要从子模块的文件开始而不是git repos,只需执行以下操作:

  • clone recursively git clone --recursive git@github.com:homer/powerplant.git (insane repo name btw) 递归克隆git clone --recursive git@github.com:homer/powerplant.git (疯狂的仓库名称btw)
  • then un-register the submodule with git config --remove-section submodule.name 然后使用git config --remove-section submodule.name取消注册子git config --remove-section submodule.name

You'll end up with what you want, submodule files in the working tree but submodules not initialized as such. 您将得到所需的结果,即工作树中的子模块文件,但子模块文件尚未初始化。

You could have used the newest git submodule deinit command, but it also removes submodule's files from the working tree. 您可能已经使用了最新的git submodule deinit命令,但是它也从工作树中删除了子模块的文件。

You can't do it directly, since submodules are never referenced by HEAD . 您不能直接执行此操作,因为HEAD绝对不会引用子模块。 It's a Git feature that prevents unwanted updated in dependencies, so submodules are referenced by the superproject at a fixed SHA1, which can be changed by committing (in the superproject) the new ones. 这是一个Git功能,可以防止依赖项中不必要的更新,因此子项目由超级项目在固定的SHA1处引用,可以通过提交(在超级项目中)新的子项目来对其进行更改。

Updating submodules to their last revision after clone is simple, though: run the following commands in the superproject. 但是,在克隆之后将子模块更新到最新版本很简单:在超级项目中运行以下命令。

git submodule foreach git merge origin/master
git commit -am "updated all submodules to origin/master"

EDIT 编辑

Looks like you want to check out submodules after the clone, command is 看起来您想在克隆后签出子模块,命令是

git submodule update --init --recursive

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

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