这可能是一种非常不寻常的情况,但我想指定在本地计算机上执行shell(git)命令时要使用的私有SSH密钥。

基本上是这样的:

git clone git@github.com:TheUser/TheProject.git -key "/home/christoffer/ssh_keys/theuser"

或者更好(用Ruby):

with_key("/home/christoffer/ssh_keys/theuser") do
  sh("git clone git@github.com:TheUser/TheProject.git")
end

我见过使用Net::SSH连接到远程服务器的示例,该服务器使用指定的私钥,但这是一个本地命令。有可能吗?


当前回答

GIT_SSH_COMMAND="ssh -i /path/to/git-private-access-key" git clone $git_repo

or

export GIT_SSH_COMMAND="ssh -i /path/to/git-private-access-key"
git clone REPO
git push

其他回答

如下所述:https://superuser.com/a/912281/607049

您可以对每个回购进行配置:

git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
git pull
git push

其他人对~/.ssh/config的建议非常复杂。它可以很简单:

Host github.com
  IdentityFile ~/.ssh/github_rsa

您需要创建一个~/.ssh/config,如下所示

Host <Your bitbucket server>
User <userid>
Hostname <Your bitbucket server as above>
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa<file> This is your private key file

许可如下

-rw------- $HOME/.ssh/config

将公钥添加到git中(cat~/.ssh/id_rsa_b[或类似名称])

然后是git clone,如下所示

git clone ssh://blahblah@blah.com/userid/test.git

2021。如果你在Mac上。

假设你在aws上有一个ubuntu服务器,你通常会这样连接:

% ssh -i blah/yourkeypair.pem ubuntu@test.fattie.com

仅在终端中

% export GIT_SSH_COMMAND="ssh -i /Users/fattie/Desktop/blah/yourkeypair.pem"

在你完成之后。然后你可以自由地。。。

% git clone ubuntu@test.fattie.com:/home/ubuntu/teste.git  

这会将服务器上的repo克隆到本地文件夹“teste”,

然后,在teste/执行通常的命令时,您可以自由地执行。。。

% git push origin master

等等

--

另请注意:https://stackoverflow.com/a/67287133/294884


至于在服务器上,似乎基本上

] git clone --bare the-actual-folder teste.git

然后在teste.git中

] git init --bare --shared

我使用zsh,不同的密钥会自动加载到我的zsh shell的ssh代理中,用于其他目的(即访问远程服务器)。我修改了@Nick的答案,并将其用于我的一个需要经常刷新的转发。(在本例中,无论我在哪里工作,我都希望在所有计算机上使用相同的最新版本的点文件。)

bash -c 'eval `ssh-agent`; ssh-add /home/myname/.dotfiles/gitread; ssh-add -L; cd /home/myname/.dotfiles && git pull; kill $SSH_AGENT_PID'

生成ssh代理向代理添加只读密钥将目录更改为我的git repo如果cd到repo目录成功,则从远程repo中拉出杀死派生的ssh代理。(我不希望有很多特工在身边徘徊。)