Why does git fail to fetch specific valid submodule for a given commit and how to fix it?

GitGit SubmodulesGit Commit

Git Problem Overview


I have a git repo which has another one as a submodule dependency. In the root of my project (where the .git, .gitsubmodules etc. are) I called

git submodule update

This failed with the following message:

> Fetched in submodule path 'src/framework', but it did not contain cc8c38e9d853491c672452d8dbced4666fc73ec8. Direct fetching of that commit failed.

where src/framework is a sub-directory of my project (PROJECT_ROOT/src/framework) and should be where the third-party repo lands. The given commit hash is a valid one.

I have also tried git clone --recursive <my-repo> but it fails too.

The contents of my .gitmodules is

[submodule "src/framework"]
        path = src/framework
        url = [email protected]:gh/framework.git

In addition to that I have to note the following important fact: due to recent updates in the framework repo my code breaks hence I really need to retrieve that specific version of it where things were working fine.

Git Solutions


Solution 1 - Git

Running this command after cloning (and receiving the error) solved my problem:

git submodule update --force --recursive --init --remote

Of course this is not a good solution. It is better to find and solve the underlying problem, but if anyone is in hurry, this was worked for me.

Solution 2 - Git

> Yes, I can follow the link in my web browser (using GitLab)

Can you clone that repo though, with that commit included?
GitLab has permission level which will restrict access, so make sure your git clone commands are executed with the right user, and with the ssh keys in said user home directory/.ssh.

If you cannot clone the submodule repo yourself (in any place on your local hard drive), that would explain the error message.

> The problem came from someone who has done a reset of the head to a commit prior to the one that was linked as a submodule in the repository I was working with. This rendered the reference invalid. I have no idea how to fix this

You can make sure the submodule follows a branch (here, for instance, master):

cd /path/to/parent/repo
git config -f .gitmodules submodule.bar1.branch master

Then update the submodule at the last fetched commit master

git submodule update --remote

The --remote option ensures that it will not use the superproject’s recorded SHA-1 to update the submodule, but will use the status of the submodule’s remote-tracking branch instead.

That would avoid the "did not contain cc8c38e9d853491c672452d8dbced4666fc73ec8" error message.


drlolly adds in the comments:

> The --remote switch was the key for me.
This worked for me: > > git submodule update --init --recursive --remote

Solution 3 - Git

My case was that the url of the submodule had changed and it was de-synchronized with the parent repo. We noticed we could clone the parent and the child would initialize without fail, but this particular instance of the repository was failing.

Fixed it by:

  1. Checking that the url was the right one in the .gitmodules file
  2. Calling git submodule sync
  3. Calling git -C path/to/submodule fetch (if the submodule was already initialized)
  4. Calling git submodule update --init [--recursive] path/to/submodule (if the submodule has been deinit)

Solution 4 - Git

I do not know what the exact problem was but below worked for me: (4th step is the key I think)

  1. git submodule update --init --recursive
  2. git pull --rebase --recurse-submodules
  3. git submodule update --force --recursive --init --remote
  4. git submodule sync
  5. git submodule update --init --recursive

Solution 5 - Git

The problem for me was that the submodule was pointing to a personal (clone of a) repository hosted on github.

I had multiple host repositories containing a reference to the same submodule. I had changed the HEAD of the submodule in one of those repositories and committed the repository. Unfortunately I had neglected to push the new submodule HEAD to github, so other instances of the repository didn't have a record of the latest head, even after git submodule update etc.

Solution 6 - Git

I started experiencing this intermittently in a (github-hosted) repo since February 2022. I never configure or edit submodules (they're just needed for building). No URLs or credentials have changed. A fresh clone suffers the same fate - it runs aground updating a random submodule. Sometimes, the error is preceded by a network/server timeout. Once this happens to a submodule, git continues to report the same problem on the same submodule, while Wireshark says it does talk to the server every time. None of the submodule sync/--init solutions helped.

To fix submodule X:

  1. Remove the X directory itself (assuming you don't have any changes in it!).
  2. Fix the corresponding directory .git/modules/X (normally) or under .git/worktrees//modules/X (if you're in an additional worktree):
    • either remove .git/modules/X entirely (it will be fetched again)
    • or refined (worked the last 3 times for me), only delete
      • .git/modules/X/shallow
      • .git/modules/X/refs/remotes/origin
  3. submodule update X

Solution 7 - Git

In my case, git config for the submodules has been updated into ssh. I updated the parent project's git config as well as its submodules to http.

Aside from that, I updated my git credentials using window's web credentials which you can search in windows settings.

Manage Web Credentials in Windows

Solution 8 - Git

When I experienced this error, it was caused by the submodule reference pointing to a commit that only existed locally.

Solution 9 - Git

I ended up just going:

  1. Going into the submodules repo
  2. git pull for latest
  3. Going into the top repo root directory
  4. git commmit -m "updated submodule" so the main repo is made aware.
  5. git push

After that my problems all disappeared. This also fixed my Netlify problems.

Solution 10 - Git

Below command solved the problem

  • git submodule sync

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrbaleksandarView Question on Stackoverflow
Solution 1 - GitAlim Giray AytarView Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - GitChnossosView Answer on Stackoverflow
Solution 4 - GitfargustoView Answer on Stackoverflow
Solution 5 - GitEoghanMView Answer on Stackoverflow
Solution 6 - GitSteinView Answer on Stackoverflow
Solution 7 - GitALBlancoIVView Answer on Stackoverflow
Solution 8 - GitFrancesco PuglisiView Answer on Stackoverflow
Solution 9 - GitmfaaniView Answer on Stackoverflow
Solution 10 - GitGuruView Answer on Stackoverflow