I have the following use case: I would like to be able to push to git@git.company.com:gitolite-admin using the private key of user gitolite-admin, while I want to push to git@git.company.com:some_repo using 'my own' private key. AFAIK, I can't solve this using ~/.ssh/config, because the user name and server name are identical in both cases. As I mostly use my own private key, I have that defined in ~/.ssh/config for git@git.company.com. Does anyone know of a way to override the key that is used for a single git invocation?
(另外:gitolite根据键来区分谁在进行推送,所以就访问、所有权和审计而言,user@server字符串对于不同的用户是相同的,这不是问题。)
正如其他人提到的,核心。sshCommand config可以覆盖SSH密钥和其他参数。
下面是一个示例,其中有一个名为~/的替代键。Ssh /workrsa,并希望将其用于~/work下克隆的所有存储库。
在~/work下创建一个新的.gitconfig文件:
[core]
sshCommand = "ssh -i ~/.ssh/workrsa"
在全局git配置中~/。gitconfig,添加:
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
以下是简单的方法:
-从你的本地根git文件夹。
-检查当前本地存储库的认证方法类型为HTTPS或SSH。
git remote -v
Example using HTTPS:
origin https://github.com/juantorres9/SQLite_crash_cours.git (fetch)
origin https://github.com/juantorres9/SQLite_crash_cours.git (push)
Example using SSH:
origin git@github.com:juantorres9/SQLite_crash_cours.git (fetch)
origin git@github.com:juantorres9/SQLite_crash_cours.git (push)
-设置您选择的身份验证方法,以防尚未设置。
For setting SSH
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
For setting HTTPS
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
-再检查一遍以防你想再次检查
git remote -v
-你现在可以使用你配置的方法来使用git push:fetch。
git push origin master
请注意
最后但并非最不重要的,检查更新的官方文件
https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories
对于git来说,除了更改配置文件,它应该使用不同的SSH密钥,如下所述:
https://stackoverflow.com/a/7927828/1306884
您可能还需要清除并重新加载活动的SSH标识。
在Mac上,执行以下操作:
ssh-add -D
ssh-add ~/.ssh/id_rsa_one_that_you_want_to_use_instead
使用这两个命令,并设置GIT URL以匹配ssh/config文件的Host中定义的字符串,应该允许您为不同的存储库使用不同的ssh密钥。
例如,对于主机work.github.com,在克隆存储库时使用work.github.com作为URL git@work.github.com:your/repository.git。