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


当前回答

以下是简单的方法:

-从你的本地根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 config配置您的存储库

git config --add --local core.sshCommand 'ssh -i <<<PATH_TO_SSH_KEY>>>'

这只适用于本地存储库。

从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文件夹。

-检查当前本地存储库的认证方法类型为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_COMMAND='ssh -i $HOME/.ssh/id_ed25519 -o IdentitiesOnly=yes -F /dev/null' git push origin c13_training_network

解释:

在执行推送之前,使用本地ENV变量 -i指定键 -F强制配置为空,因此全局配置不会覆盖此临时命令