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配置,而不仅仅是在回购级别。

在我的例子中,所有的项目都在~/work/目录下,所以我更新了我的gitconfig如下:

# ~/.gitconfig
...

[includeIf "gitdir:~/work/**"]
  path = "~/work/.gitconfig"
# ~/work/.gitconfig
[user]
  email = amr@work.com
  name = Amr Awad
  sshCommand = ssh -i ~/.ssh/work

现在~/work/目录下的所有回购操作都使用工作键~/。Ssh /work,不需要分别配置每个repo。

其他回答

一个基于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文件中添加任意多的键。

如果在windows上使用Git版本的ssh, ssh配置中的身份文件行如下所示

IdentityFile /c/Users/Whoever/.ssh/id_rsa.alice

其中/c代表c:

要检查,在git的bash做

cd ~/.ssh
pwd 

另一种替代方法是使用ssh-ident来管理您的ssh身份。

它会根据您当前的工作目录、ssh选项等自动加载和使用不同的密钥……这意味着您可以轻松地拥有一个工作/目录和一个私有/目录,它们在ssh中透明地使用不同的密钥和身份。

以下是简单的方法:

-从你的本地根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环境变量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等。