2

Say I have some big repo, BigRepo, and I want to add it as a submodule for another project. Normally I would do

$ git submodule add [email protected]/me/BigRepo.git 

but I don't want to download the whole thing again. Luckily I already have a checkout of it, so I try

$ git clone ../BigRepo Cloning into 'BigRepo'... done. $ git submodule add [email protected]/me/BigRepo.git Adding existing repo at 'BigRepo' to the index 

Great! Except

  1. The origin remote in the submodule points to ../BigRepo; I'd like it to point to [email protected]/...
  2. The .git folder is in BigRepo/.git instead of .git/modules/BigRepo

So what's the right way to do this?

1 Answer 1

1

There's going to be a little fixup to do because you're wanting to discard all the evidence of that proxy repo (../BigRepo), but it's not hard.

Easiest from here is probably to just redo the submodule add with what you have, do that by

# undo the in-repo add mv BigRepo ../agggh_I_meant_to_add_this_from_outside git rm --cached BigRepo git config -f .gitmodules --remove-section submodule.BigRepo git config --remove-section submodule.BigRepo 

And your backout is complete.

Now, if you add a submodule from a local, unpublished repo you have to then correct the urls later for the convenience of others, but that's straightforward:

# Get `git submodule add` to do the hoisting for you git submodule add ../agggh_* BigRepo # but from now on use u://r/l as the submodule's published repo: git config -f .gitmodules --set submodule.BigRepo.url u://r/l git config --set submodule.BigRepo.url u://r/l # alternate spelling of `git submodule sync BigRepo` here # and the published repo is my copy's `origin` too git --git-dir=BigRepo/.git remote set-url origin u://r/l 

(

edit addressing questions in comments:

The mv/clone-from-the-mv'd-one pair is because perhaps you've done, or done and forgotten, some work there, and so ../BigRepo might be out-of-date. It's just a safety play; a rm -rf BigRepo and later git submodule add ../BigRepo would work as well if the currently-added one is really discardable.

I can't figure how any of the submodule adds above could provoke network activity -- ../anything is on the same filesystem as ../ThisRepo i.e. ., right? git clone of a plain same-filesystem path, relative or absolute, does dirt-cheap hardlinks unless you take steps to provoke an actual copy -- this works even on NTFS. It's only if you specify a full url or otherwise force the issue that git creates a full-copy clone. Maybe it's just the caffeine kicking in unusually slowly, if I'm missing something please, whoever sees it, just edit it in or edit this whole paragraph out.

)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this works great! But assuming I have some more submodules to add in the same way, what's the best way to do this from a clean slate, so to speak? git submodule add ../BigRepo re-clones it over the network. Is there a better way than cloning ../BigRepo to ../BigRepoCopy and then adding it?
Actually never mind, turns out git treats ../BigRepo as relative to origin. If you do git submodule add /full/path/to/BigRepo it works as expected.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.