Can I make a “deep copy” of a git repository with

2020-07-25 23:28发布

问题:

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? 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.

回答1:

If you want to start with submodules' files but not as git repos, simply do the following:

  • clone recursively git clone --recursive git@github.com:homer/powerplant.git (insane repo name btw)
  • then un-register the submodule with 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.



回答2:

You can't do it directly, since submodules are never referenced by 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.

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