4

I have two git identities, one is a personal one and the other is for my employer.

My work project uses a submodule and although I can clone the main repo fine, I am not able to update the submodule. What do I need to configure so the submodule can also be updated locally without getting the following error?

Fetching origin
From github.com:/work_domain/work_submodule
 * branch            HEAD       -> FETCH_HEAD
error: pathspec 'master' did not match any file(s) known to git.

I have 2 sets of id_rsa keys in my ~/.ssh directory:

id_rsa.pub <= personal ssh key
id_rsa_work.pub <= work ssh key

~/.ssh/config file:

#work acccount
Host github-work
  HostName github.com
  User git (corrected with info from answers)
  IdentityFile ~/.ssh/ida_rsa_work

#personal account
Host github-personal
  HostName github.com
  User git
  Identity ~/.ssh/ida_rsa

When I initially cloned my work repo successfully, I used the adjusted host mapping:

git clone git@github-work:work_domain/repo_name.git

instead of what I normally use when at work:

git clone git@github.com:work_domain/repo_name.git

Within the work project repo, the .gitmodules file of course has the official mapping:

[submodule "work_submodule"]
        path = work_submodule
        url = git@github.com:/work_domain/work_submodule.git

Per suggestion below, I updated the .gitmodules attribute to be:

[submodule "work_submodule"]
        path = work_submodule
        url = git@github-work:/work_domain/work_submodule.git

But still can't update the submodule locally.

Jonas Gardner
  • 2,458
  • 5
  • 22
  • 28

2 Answers2

2

To guard against the possibility of accidentally committing your private URL for the submodule to the repository (which is dependent on your .ssh/config), you should instead edit the url in .git/ssh


Editing the URL in .gitmodules file to use github-work is one way of doing it. However since that's a file part of the project, it might be committed by mistake.

Instead as @Chronical mentioned in a comment. To only change the local settings for your particular checkout, you should instead change the URL in .git/config

url = github-work:/work_domain/work_submodule.git

By changing it there instead of in .gitmodules you can get rid of the extra step to cleanup stuff that accidentally used the incorrect URL

Note that if you do git submodule sync the URL you edit in .git/config is overwritten with the one in .gitmodules

Joakim
  • 11,468
  • 9
  • 44
  • 50
0

First, if you have a ~/.ssh/config, it is to be able to use different public/private keys, which means your ssh url should use said config file:

 url = github-work:/work_domain/work_submodule.git
 # or
 url = github-personal:/work_domain/work_submodule.git

The all point of the config file is to use different keys, and to not have to mention the user, the port number, the server name and so on.
'github-work' and 'github-personal' are entry keys in that config file, they are not server names.

Second, after modifying the url in the .gitmodules file, don't forget to do:

  • completely remove your submodule content if it was already loaded from the previous address:
rm -Rf work_domain/work_submodule ; git checkout -- work_domain/work_submodule
  • update again that submodule:
git submodule sync
git submodule update work_submodule

See also, with Git 2.25 (Q1 2020) "Git submodule url changed" and the new command:

git submodule set-url [--] <path> <newurl>
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250