how to update submodule url in all commits
Asked Answered
N

3

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?

Nyctaginaceous answered 16/8, 2012 at 12:22 Comment(1)
Note: Git 2.25 (Q1 2020) comes with a new command": git submodule set-url [--] <path> <newurl>Panache
F
11

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.

Foreandafter answered 16/8, 2012 at 12:43 Comment(5)
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?Nyctaginaceous
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.Nyctaginaceous
@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.Foreandafter
@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.Foreandafter
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).Nyctaginaceous
P
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.

Provocation answered 29/11, 2016 at 13:25 Comment(0)
N
0

Usually there is no reason to compile an older version of a project (the version which refers to the submodule with an old or even already non-existent URL). But even one has to do that, one can always create a new branch that origins from this version (commit) and add a new commit to this new branch which would correct the URL.

Nebulosity answered 24/11, 2023 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.