在特定 git 存储库中指定密钥文件

Specify key file in a specific git repository

我使用的是协作用户,因此无法更改全局文件夹。我有一个与多个遥控器相连的 git 存储库。我需要使用不同的 rsa 密钥访问不同的遥控器。是否可以在本机 git 中插入关键路径,例如也许在 .git/config 里面,所以我在做克隆、获取、推送、拉取时不需要重复输入关键路径?

编辑:我知道类似的问题,例如 Specific RSA keys per git repo,但它没有提供 "per repo" 的确切答案。

您需要replace the ssh url of your submodules by ssh url u~指的是全局用户的$HOME

您的 ~/.ssh/config 将包含相关私钥的路径:

Host repo1
  HostName git.myhost.lan
  User git
  IdentityFile /path/to/global/user/.ssh/repo1PrivateKey

对于子模块repo1

git config --file=.gitmodules submodule.repo1 .url repo1:user/repo1
# if needed to follow a branch (if not, skip it)
git config --file=.gitmodules submodule.repo1 .branch abranch
git submodule sync
git submodule update --init --recursive --remote
git add .
git commit -m "Change submodule repo1 url"
git push

注:git config+git submodule sync可以替换
请参阅“”和新命令(Git 2.25,2020 年第一季度)

git submodule set-url [--] <path> <newurl>