Home > database >  Git errors out when a submodule in the repo is switched to a different repo with other tree structur
Git errors out when a submodule in the repo is switched to a different repo with other tree structur

Time:10-15

I have a git repo that uses a submodule A, and for technical reasons I need to switch the submodule to a submodule B, which is a different repo with different commits, they only shared a couple of commits from the beginning of the project, which is okay to me.

What I've done so far is:

  1. Modify the.gitmodules file to use the new URL
  2. Delete the submodule folder in the repo rm -rf .git/modules/<submodule>
  3. Delete the submodule folder in the working directory rm -rf <submodule>
  4. Run git submodule sync
  5. Run git submodule update

After that I get an error:

fatal: remote error: upload-pack: not our ref a5129baec669f7736552019baccd3da7e5129cfd
Fetched in submodule path 'Submodule/path', but it did not contain a5129baec669f7736552019baccd3da7e5129cfd. Direct fetching of that commit failed.

So the commit with the SHA a5129b exists in the submodule A as the most recent commit, but is not in the submodule B.

I don't understand why is trying to fetch the commit from the previous submodule, am I missing something?

CodePudding user response:

I managed to reproduce this and fixed it with these commands:

git submodule set-url <submodule> <old url>
git submodule update
git submodule set-url <submodule> <new url>
git submodule update
git -C <submodule> fetch
git -C <submodule> reset --hard origin/HEAD

WARNING: This will erase any uncommitted changes in submodule!

  • Related