3

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.

Anne van Rossum
  • 3,091
  • 1
  • 35
  • 39
  • possible duplicate of [Need to handle git-submodules in git-archive](http://stackoverflow.com/questions/1591387/need-to-handle-git-submodules-in-git-archive) – CharlesB Oct 21 '13 at 22:32
  • I am not interesting in archiving. I just want a deep copy of a git repository where the submodule structure is gone and replaced by the corresponding code. I rather need ```git flatten``` if it would exist. – Anne van Rossum Nov 20 '13 at 11:13
  • Yes, [How do I git clone --recursive and checkout master on all submodules in a single line?](http://stackoverflow.com/q/6474847) is better – CharlesB Nov 20 '13 at 12:26
  • No, that is also something else. The submodule structure will not disappear because you checkout and `bzr` will still not understand it. I really want a flattened out copy, not just an ordinary clone. – Anne van Rossum Nov 20 '13 at 12:33

2 Answers2

4

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.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
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
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • Thanks, but the problem is not that I do not know how to update the repository with submodules. I want a copy of that repository where the submodules have been replaced by their code. Or else `bzr` will not be able to import it. – Anne van Rossum Oct 21 '13 at 15:45
  • Got it, see this question: [How do I git clone --recursive and checkout master on all submodules in a single line?](http://stackoverflow.com/q/6474847) (I included the correct answer in mine, not all are correct) – CharlesB Oct 21 '13 at 15:50
  • Yes, it is a different question. I think the only solution in my case, where I do not want to have any submodules in the target repository is by circumventing git entirely (and loosing the history): `rsync -avzul source-repos/ --exclude=.gitmodules --exclude=.git dest-repos/`. Now in the final repository, I do not have the submodules anymore. Compare https://github.com/dobots/aim with https://github.com/dobots/aim-bzr. – Anne van Rossum Oct 21 '13 at 16:03
  • OK; so you want a dump of the repository, without the `.git` repo, and including the submodules, right? – CharlesB Oct 21 '13 at 16:57
  • 1
    I think that what you're after is https://github.com/meitar/git-archive-all.sh/wiki (from [How do I git clone --recursive and checkout master on all submodules in a single line?](http://stackoverflow.com/q/6474847) – CharlesB Oct 21 '13 at 16:58
  • 1
    Nice find. It might indeed be enough to run git-archive recursively. However, in that case I prefer running `rsync` myself (as above), so I know what will happen exactly. It's a one-liner, so not a lot of trouble. I just thought there might have been a `git` option I missed. – Anne van Rossum Oct 21 '13 at 22:17
  • Sure rsync can do the job here. Not sure about advantages of such a script, but there must be things it is more aware of. – CharlesB Oct 21 '13 at 22:33