# Possible bug in Git submodule update --inti

3 messages from 2024-11-18 to 2024-11-19. Participants: bech, Philippe Blain, Junio C Hamano.
Thread: https://gitlist.dev/t/62510

## bech, 2024-11-18 08:14

Subject: Possible bug in Git submodule update --inti
Message-ID: <DB9PR10MB709879A45AABEC017F3E89DF8A272@DB9PR10MB7098.EURPRD10.PROD.OUTLOOK.COM>
URL: https://gitlist.dev/e/DB9PR10MB709879A45AABEC017F3E89DF8A272%40DB9PR10MB7098.EURPRD10.PROD.OUTLOOK.COM

```

Hi

I am trying to replace a submodule of a repository with a rewritten version.
Somebody added a huge json file without enabling lfs, so I cannot move the submodule to GitHub without the rewrite.

The path of the submodule is: ./Submodules/Lodam.Standard, which I have no intensions of changing.

The url of the original submodule is: https://lodamrd.visualstudio.com/Features.Net/_git/Lodam.Standard
The url of the rewritten submodule is: https://lodamrd.visualstudio.com/Features.Net/_git/Uniware.Standard


I planned to do a:
git submodule deinit -f <path-to-submodule>
git submodule set-url -- <path-to-submodule> <new-url>
git submodule update --init <path-to-submodule>

But actually the sources for the submodule is fetched from the original (same commit SHAs  as before), whereas if I do:

git submodule deinit -f <path-to-submodule>
git submodule set-url -- <path-to-submodule> <new-url>
git clone <new-url> <path-to-submodule>

I get the submodule from the new url with the rewritten commit SHA’s.

I have attached the PowerShell output with trace information. I am unfamiliar with git trace, but from line 203 I can see the old url mentioned, where I would expect the new url.

Best regards

Søren Bech Christensen 
Software Engineer 
BITZER Electronics A/S
PC Software Team 
Kærvej 75
6400 Sønderborg // Denmark
Direct:	+45 73 42 37 70 
Phone:	+45 73 42 37 37 
Mobile:	+45 25 44 49 65 
Email:	Soeren.Bech@bitzerdk.com 


```

## Philippe Blain, 2024-11-18 19:22

Subject: Re: Possible bug in Git submodule update --inti
Message-ID: <8db6eba3-a151-3819-159b-921a6c3f3242@gmail.com>
URL: https://gitlist.dev/e/8db6eba3-a151-3819-159b-921a6c3f3242%40gmail.com
In-Reply-To: <DB9PR10MB709879A45AABEC017F3E89DF8A272@DB9PR10MB7098.EURPRD10.PROD.OUTLOOK.COM>

```
Hi Søren,

Le 2024-11-18 à 03:14, Bech Christensen, Søren a écrit :
> 
> Hi
> 
> I am trying to replace a submodule of a repository with a rewritten version.
> Somebody added a huge json file without enabling lfs, so I cannot move the submodule to GitHub without the rewrite.
> 
> The path of the submodule is: ./Submodules/Lodam.Standard, which I have no intensions of changing.
> 
> The url of the original submodule is: https://lodamrd.visualstudio.com/Features.Net/_git/Lodam.Standard
> The url of the rewritten submodule is: https://lodamrd.visualstudio.com/Features.Net/_git/Uniware.Standard
> 
> 
> I planned to do a:
> git submodule deinit -f <path-to-submodule>

Here you deinit the submodule, such that the "submodule Submodules/Lodam.Standard" section is 
removed from the config file of the superproject: this submodule is not considered
active anymore.

> git submodule set-url -- <path-to-submodule> <new-url>

Here you use 'set-url' to change the submodule URL. The man page states [1]:
"Sets the URL of the specified submodule to <newurl>. Then, it will automatically 
synchronize the submodule’s new remote URL configuration."

In your case, this modifies the 'url' config in the 
"submodule Submodules/Lodam.Standard" section of your .gitmodules. 
However it does not do anything else, since the submodule 
was deinitialized above. This is mentioned in the doc for 
'git submodule sync' [2]: 
"It will only affect those submodules which already have a URL entry 
in .git/config (that is the case when they are initialized or freshly added)."

> git submodule update --init <path-to-submodule>

This re-initializes your submodule, which copies the new URL from .gitmodules
to the 'url' config in the "submodule Submodules/Lodam.Standard"
section of your '.git/config', as mentioned
in the doc for 'git submodule init' [3]. Note that this is _not_ 
the exact command that you ran according to the trace output below, which is:

> D:\temp\Mjolnir [develop ≡ +0 ~1 -0 !]>  git submodule update --init --remote ./submodules/Lodam.Standard

Notably the '--remote' flag changes what happens next:
- without --remote, Git tries to checkout the submodule commit recorded in the superproject
- with --remote, Git tries to checkout the commit at the tip of the remote HEAD of the submodule [4]

I'll discuss what happens with '--remote' since this is what you appear to be using.

After writing the url to '.git/config' as noted above, Git checks if
it needs to clone the submodule. In your case it does find a Git repository for the 
submodule at .git/modules/Lodam.Standard, so it goes on to the next step.
Next it fetches the submodule remote to get the latest submodule commits
(since --remote was passed). We can see in the trace output that 'git fetch'
is invoked. This will thus fetch the default remote in the submodule 
(usually called "origin"). The URL used for this fetch is the value of
'remote.origin.url', which _still_ points to https://lodamrd.visualstudio.com/Features.Net/_git/Lodam.Standard
because none of the previous commands have updated it.

> But actually the sources for the submodule is fetched from the original (same commit SHAs  as before), whereas if I do:
> 
> git submodule deinit -f <path-to-submodule>
> git submodule set-url -- <path-to-submodule> <new-url>
> git clone <new-url> <path-to-submodule>
> 
> I get the submodule from the new url with the rewritten commit SHA’s.

If you manually do a 'git clone' you indeed to get the new objects from
the new URL, but the Git repository is not put into .git/modules/Submodules/Lodam.Standard
as is the case with 'git submodule'. Plus, you still have the old repository at 
.git/modules/Submodules/Lodam.Standard, so it is not a good solution in my opinion.

I think what you'd want to do is simply:

git submodule set-url -- <path-to-submodule> <new-url> 
git submodule update --init --remote

This will fetch the new URL and checkout the tip commit of its HEAD branch.
You will still have all the objects from the old URL locally though. If you want
to avoid that, you should completely remove the Git repository of the submodule:

git submodule set-url -- <path-to-submodule> <new-url> 
git submodule deinit -f <path-to-submodule>
rm -rf $(git rev-parse --git-dir)/modules/Submodules/Lodam.Standard
git submodule update --init --remote

Hope this helps,
Philippe.


[1] https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-set-url--ltpathgtltnewurlgt
[2] https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-sync--recursive--ltpathgt82308203
[3] https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-init--ltpathgt82308203
[4] https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt---remote

```

## Junio C Hamano, 2024-11-19 00:32

Subject: Re: Possible bug in Git submodule update --inti
Message-ID: <xmqqldxghxml.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqldxghxml.fsf%40gitster.g
In-Reply-To: <8db6eba3-a151-3819-159b-921a6c3f3242@gmail.com>

```
Philippe Blain <levraiphilippeblain@gmail.com> writes:

> Hi Søren,
> ...
> in the doc for 'git submodule init' [3]. Note that this is _not_ 
> the exact command that you ran according to the trace output below, which is:
> ...
> I think what you'd want to do is simply:
>
> git submodule set-url -- <path-to-submodule> <new-url> 
> git submodule update --init --remote
>
> This will fetch the new URL and checkout the tip commit of its HEAD branch.
> You will still have all the objects from the old URL locally though. If you want
> to avoid that, you should completely remove the Git repository of the submodule:
>
> git submodule set-url -- <path-to-submodule> <new-url> 
> git submodule deinit -f <path-to-submodule>
> rm -rf $(git rev-parse --git-dir)/modules/Submodules/Lodam.Standard
> git submodule update --init --remote

Thanks for a pleasant exchange, RFH with sufficient information
necessary to help, with a detailed diagnosis and an alternative
offered.

```
