Search code examples
gitsshgit-submodules

Access problem with private Git submodule using SSH


I have 2 private Git repositories on GitHub: game-engine and game.

I want to have game-engine as a submodule in my game repository. I have created both repositories, and pushed them to GitHub. My remotes are set up using SSH authentication and that works fine.

In both local repositories I have added my private ssh-key like so:

git config --local core.sshCommand "ssh -i <path-to-ssh-key>"

This has always worked for accessing my private remote repositories (e.g. now I can just use push and pull).

I then try to add game-engine as a submodule to game using the SSH url:

git submodule add git@github.com:maltebp/game-engine.git

But I get this error:

Cloning into 'C:/Users/malte/projects/game/game-engine'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: clone of 'git@github.com:maltebp/game-engine.git' into submodule path 'C:/Users/malte/projects/game/game-engine' failed

Do I have to pass in the ssh-key manually or something?

I have found very little about this online so I don't if this is something that people rarely do?


Solution

  • As you showed in your setup: you defined the core.sshCommand only locally (the --local in git config --local ...), which means that this option is written to the repository-scoped .git/config file.

    This configuration file is not taken into account when running git submodule add ... so the underlying clone command does not use your customized ssh -i <path-to-ssh-key> command.

    One way to circumvent that is: make that customized available during the git submodule add ... command

    • you can override a configuration option for the duration of one single git command using git -c ...:
      git -c core.sshCommand "ssh -i <path-to-ssh-key>" submodule add ...
    • or use the GIT_SSH environment variable:
      GIT_SSH="ssh -i <path-to-ssh-key>" git submodule add ...

    If you want to always use your dedicated ssh key for repos under github.com/maltebp, you can set up an alias in .ssh/config, then set a url.<base>.insteadOf to "catch" accesses to github.com/matebp and rewrite them using that alias.