这可能是一种非常不寻常的情况,但我想指定在本地计算机上执行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/config(或c:\users\<youruser>\.ssh\config)并指定多个身份,这也支持同一主机的不同用户(例如,个人GitHub帐户和工作用户),这也适用于Windows:
Host github.com
HostName github.com
IdentityFile /path/to/your/personal/github/private/key
User dandv
Host github-work
HostName github.com
IdentityFile /path/to/your/work/github/private/key
User workuser
然后,要将项目克隆为您的个人用户,只需运行常规gitclone命令即可。
要将repo克隆为工作用户,请运行gitclonegit@github-work:公司/项目.git。
如果这里的其他解决方案都不适合您,并且您已经创建了多个ssh密钥,但仍然无法执行以下简单操作
git pull
那么假设您有两个ssh密钥文件
id_rsa
id_rsa_other_key
然后在git repo中,尝试:
# Run these commands INSIDE your git directory
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_other_key
并通过以下方式确保github默认用户名和userid正确:
# Run these commands INSIDE your git directory
git config user.name "Mona Lisa"
git config user.email "mona.lisa@email.com"
看见https://gist.github.com/jexchan/2351996了解更多信息。
这样的方法应该有效(orip建议):
ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git'
如果您更喜欢子壳,可以尝试以下方法(尽管它更脆弱):
ssh-agent $(ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git)
Git将调用SSH,SSH将通过环境变量查找其代理;这将反过来加载密钥。
或者,如果您愿意将仅包含.ssh目录的目录设置为HOME,那么设置HOME也可以达到目的;这可能包含identity.pub或配置文件设置IdentityFile。
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