Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use different git submodule URL's on different machines

Tags:

git

Is there any way to use one submodule URL when on one machine, but use a different submodule URL when on the other?

Let's assume I have one machine which has authenticated SSH access to a git repository on a central server and another machine which does not have access to that server. On the authenticated machine I have used git-daemon to share the repositories to my non-authenticated machine.

The problem here is that the repository I've cloned has several submodule dependencies, all of which are repositories on the server that requires authenticated SSH access. In this case, I also have all of the submodules stored on my authenticated machine, so I can simply update the .gitmodules file from:

[submodule "@sub-a"]
    url = ssh://<authenticated-url>/<repo>
    branch = master

to...

[submodule "@sub-a"]
    url = git://<ip-of-authenticated-machine>/<repo>
    branch = master

Then running git submodule sync and git submodule update --init -r will fetch the submodules directly from my authenticated machine instead of from the server.

My question then is this, is there any portable way to do this so that I can freely move between the machines and expect correct git-submodule behavior?

like image 568
Michael Leonard Avatar asked Dec 08 '16 17:12

Michael Leonard


2 Answers

You can change submodule properties in .git/config. In your case you can change url from [submodule "<name>"] section. Keep in mind that those properties won't survive git submodule deinit, git submodule sync
Detailed

git submodule deinit     # deinitialize submodule, relevant section in .git/config will be removed
rm -fdr .git/modules/    # clear submodules' local repos
git submodule init       # relevant section will be copied (partially) from .gitmodules to .git/config
...                      #Edit .git/config with new url for submodule
git submodule update     # submodule will be updated from new url.
like image 190
IrLED Avatar answered Nov 14 '22 22:11

IrLED


Would it count as portable if it came with instructions?

You can have your submodule in .gitmodules point to a local directory, like url = ../submodule. The main repository would contain this path (same for the two machines) but they would have cloned something else to their respective submodule directories, created in the same directory as the clone of the main repo. A submodule update would understandably have to be done in two steps: a normal fetch from the server to submodule and then a submodule update from the local clone to the main module.

You may also want to see a related question where the same solution was proposed here. I'm not aware of a better way.

Edit: as identified by IrLED in their answer an url in .git/config overrides that .gitmodules (see here) and is local to each clone (not tracked, not bound to a commit), so that makes it the perfect solution. The change can be done after the (nonrecursive) clone of the main repo and before the submodule update. All credits there!

like image 35
The Vee Avatar answered Nov 14 '22 23:11

The Vee