6

The Use Case is that i have to move certain repositories to a new server. So these repositories get a new url.
The Parent project which reference these sub-modules needs to be updated with the new url for the sub-module.
I think of doing the following.

  1. update the .gitmodules file
  2. git submodule sync
  3. git submodule update
  4. commit and push

But, since the previous commits have the earlier version of the .gitmodule, if i checkout a previous commit of the parent project - will it not look for the old server?


To ensure reproducibility, we need to have all old commits to be working. Any idea to get around this?

maxmelbin
  • 2,045
  • 3
  • 21
  • 29
  • Note: Git 2.25 (Q1 2020) comes with [a new command](https://stackoverflow.com/a/59364554/6309)": `git submodule set-url [--] ` – VonC Dec 16 '19 at 21:36

2 Answers2

10

The URL that's in .gitmodules is generally only used when initializing the submodules or on git submodule sync. On initialization (git submodule init), the URL is put into the repository's .git/config, and when the submodule is cloned into place (on git submodule update) the URL to use is taken from the config. The only other time the URL in .gitmodules is used is when you run git submodule sync, which will similarly update the URL in the config, but also set the origin remote in the submodule to the same URL.

This means that you won't have any problems with checking out an earlier commit and running git submodule update - the remote origin in your submodule isn't changed when you checkout a new commit in the parent repository.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • when i clone a parent project, i have to give submodule init. Else the submodules are not accessible. So it still depends on .gitmodules right? or am i getting it totally wrong? – maxmelbin Aug 17 '12 at 04:31
  • i modified the .gitmodules, submodule sync and pushed it. The latest version is pointing to the new URLs alright. Now i pulled a branch from an earlier version, here the submodule update is looking for the old URL. – maxmelbin Aug 17 '12 at 04:46
  • @maxmelbin: in answer to your first comment, yes - on cloning the parent project one would have to initialize the submodules, and the URLs would be taken from `.gitmodules` for that, as described in my answer. – Mark Longair Aug 17 '12 at 09:47
  • @maxmelbin: on your second point, you'll have to explain exactly which commands you ran for what you described as: "pull a branch from an earlier version, here the submodule update is looking for the old URL". If the "earlier version" was an existing repository whose submodules were initialized and updated, then pulling the latest version won't change `origin` in each submodule until you do `git submodule sync`. – Mark Longair Aug 17 '12 at 09:52
  • here is what i did. i created a branch from an earlier commit.Then i cloned a new copy, from this branch. Now if i do a sub-module init it intialises the submodule from the old URL (in this case the old URL also works as i dint shut down that server yet). – maxmelbin Aug 21 '12 at 12:34
0

If you need to do that the only way to go is use filter-branch.

But be carefull because changing .gitmodules on all commits implies that you transform that commits.

If you have the git repo shared with a lot of developers all developers need to "force pull" the new commits and all work based on old commits need to be rebased to new branch.

There are a lot of discussions about rewriting git history.

Isaac Pascual
  • 472
  • 2
  • 14