Search code examples
gitgit-submodulesgit-history

Changing the remote repository of git submodule for all commits in git history


How can I change the remote url of a git submodule without breaking commit history? Currently if I checkout old commits my submodule seems to have the old remote url witch doesn't exist anymore.

My workflow:

  1. Clone repo with commit A,B
  2. git submodule update --init
  3. Change submudule url
  4. Push .gitmodules to remote (Commit C)

  5. Clone repo with commit A,B,C

  6. Switch to commit A
  7. git submodule update --init -> FAILES

-> want also the new submodule remote URL, but still the old url in .gitmodules

It seems that git submodule update --init is still trying to use the old remote url

If have also tried to use git submodule update , as suggested, but I also got the error:

The Git repository with name or identifier OLD_REPO_NAME does not exist or you do not have permissions for the operation you are attempting.


Solution

  • Let's try another approach. Instead of replacing URLs in the config files replace URLs on the fly:

    git config --global url.<NEW-URL>.insteadOf <OLD-URL>
    

    See git help config about url.<base>.insteadOf.