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字符串对于不同的用户是相同的,这不是问题。)


当前回答

我已经在github上测试了以下方法,基于阅读其他答案,它结合了一些技巧:

正确的SSH配置 git URL重写

这种方法的优点是,一旦设置好,它不需要任何额外的工作来使它正确-例如,您不需要更改远程URL或记住以不同的方式克隆东西- URL重写使它全部工作。

~ / . ssh /配置

# Personal GitHub
Host github.com
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/github_id_rsa

# Work GitHub
Host github-work
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/work_github_id_rsa

Host *
  IdentitiesOnly yes

~ / . gitconfig

[user]
    name = My Name
    email = personal@personal.email

[includeIf "gitdir:~/dev/work/"]
    path = ~/dev/work/.gitconfig

[url "github-work:work-github-org/"]
    insteadOf = git@github.com:work-github-org/

~ / dev /工作/ gitconfig。

[user]
    email = work@work.email

只要你把所有的工作reppos放在~/dev/work和其他地方的个人物品下,git在向服务器进行拉/克隆/推操作时将使用正确的SSH密钥,并且它还将为你的所有提交附加正确的电子邮件地址。

引用:

1 2

其他回答

一个基于Unix的系统(Linux、BSD、Mac OS X),默认标识存储在$HOME/目录下。Ssh,分2个文件: 私钥:$HOME/.ssh/id_rsa 公钥:$HOME/.ssh/id_rsa.pub 当您使用没有选项-i的ssh时,它将使用默认的私钥对远程系统进行身份验证。

如果您有另一个想要使用的私钥,例如$HOME/。Ssh /deploy_key,您必须使用Ssh -i ~/。ssh / deploy_key……

这很烦人。您可以在$HOME/中添加以下行。bash_profile: ssh-add ~ / . ssh / deploy_key ssh-add ~ / . ssh / id_rsa

因此,每次使用ssh、git或scp(基本上也是ssh)时,都不必再使用option -i了。

您可以在$HOME/.bash_profile文件中添加任意多的键。

你可以使用git环境变量GIT_SSH_COMMAND。在你的终端git仓库下运行:

GIT_SSH_COMMAND='ssh -i ~/.ssh/your_private_key' git submodule update --init

取代~ /。Ssh /your_private_key与你想使用的Ssh私钥路径。你可以将后续的git命令(本例中是git子模块update——init)更改为其他命令,如git pull、git fetch等。

从git 2.10开始,也可以使用gitconfig sshCommand设置。文档状态:

如果设置了这个变量,当git fetch和git push需要连接到远程系统时,它们将使用指定的命令而不是ssh。该命令与GIT_SSH_COMMAND环境变量的形式相同,在设置环境变量时将被覆盖。

一个使用示例是:git config core。ssh -i ~/.ssh/[insert_your_keyname]

在某些情况下,这行不通,因为ssh_config覆盖了命令,在这种情况下,尝试ssh -i ~/。ssh/[insert_your_keyname] -F /dev/null不使用ssh_config. sh

对于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。

以下是简单的方法:

-从你的本地根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