[英]How do I push to two different GitHub accounts using SSH keys in Powershell?
I'm on Windows 10 and I have Git for Windows installed.我使用的是 Windows 10,并且安装了适用于 Windows 的 Git。 I'm pushing code to two different GitHub accounts, and I'm trying to set up my SSH agent to use two keys (one for each account).
我正在将代码推送到两个不同的 GitHub 帐户,并且我正在尝试设置我的 SSH 代理以使用两个密钥(每个帐户一个)。
Here's my ~\\.ssh\\config
:这是我的
~\\.ssh\\config
:
Host github.com-userone
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
Host github.com-usertwo
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_usertwo
It works correctly if I push/pull using Git Bash, but I want to be able to use Powershell also.如果我使用 Git Bash 推/拉它可以正常工作,但我也希望能够使用 Powershell。 When I try to do a push or pull from Powershell, I get the "please make sure you have the correct access rights" error.
当我尝试从 Powershell 执行推送或拉取操作时,出现“请确保您具有正确的访问权限”错误。 If I run
start-ssh-agent.cmd
from Powershell, then git push
works for that instance of Powershell.如果我从 Powershell 运行
start-ssh-agent.cmd
,则git push
适用于该 Powershell 实例。
If I add start-ssh-agent.cmd
to my profile.ps1
profile then the SSH agent starts and git push
works correctly, but that seems to break other aspects of my Powershell profile (shown below).如果我将
start-ssh-agent.cmd
添加到我的profile.ps1
配置文件中,则 SSH 代理启动并且git push
正常工作,但这似乎破坏了我的 Powershell 配置文件的其他方面(如下所示)。 The Import-Module
and Set-Theme
no longer work. Import-Module
和Set-Theme
不再起作用。 I don't get any errors;我没有收到任何错误; I just no longer get my posh-git or oh-my-git styling.
我只是不再有我的 posh-git 或 oh-my-git 样式。
Import-Module oh-my-posh
Import-Module posh-git
Set-Theme Paradox
Set-Location "c:\users\brubin\documents\visual studio 2017\projects"
start-ssh-agent.cmd
Most of the instructions that I've found for getting this to work involve running eval $(ssh-agent -s)
, however eval
doesn't work in Powershell.我发现的大部分指令都涉及运行
eval $(ssh-agent -s)
,但是eval
在 Powershell 中不起作用。
How do I set this up so that I can push/pull from two different GitHub accounts using Powershell?如何设置它以便我可以使用 Powershell 从两个不同的 GitHub 帐户推送/拉取?
First, you need ssh-agent only if you have protected those private keys with a passphrase when creating them.首先,仅当您在创建私钥时使用密码保护这些私钥时,才需要 ssh-agent。
Second, make sure to use the right SSH URL form, referencing those ~/.ssh/config entries:其次,确保使用正确的 SSH URL 形式,引用那些 ~/.ssh/config 条目:
github.com-usertwo:<usertwo>/myRepo
As mentioned in dahlbyk/posh-git
issue 640如
dahlbyk/posh-git
问题 640 中所述
Rather than use Git's
start-ssh-agent.cmd
, I would use the built-in OpenSSH you have on Windows 10 1803.我不会使用 Git 的
start-ssh-agent.cmd
,而是使用 Windows 10 1803 上的内置 OpenSSH。
To use this follow these steps:要使用它,请按照下列步骤操作:
From a PowerShell window run
get-command ssh.exe
.从 PowerShell 窗口运行
get-command ssh.exe
。 You should see thatssh.exe
is located atC:\\Windows\\System\\OpenSSH\\ssh.exe
.您应该会看到
ssh.exe
位于C:\\Windows\\System\\OpenSSH\\ssh.exe
。
If you don't get any result, open Apps & Features section of the Windows 10 Settings app and press the "Manage optional features" link.如果您没有得到任何结果,请打开 Windows 10 设置应用程序的应用程序和功能部分,然后按“管理可选功能”链接。 Once that is open select OpenSSH Client to add that feature.
打开后,选择 OpenSSH Client 以添加该功能。 After that has finished, open PowerShell again and see if
get-command ssh.exe
returnsC:\\Windows\\System\\OpenSSH\\ssh.exe
完成后,再次打开 PowerShell,查看
get-command ssh.exe
返回C:\\Windows\\System\\OpenSSH\\ssh.exe
Now let's start the ssh-agent service and configure it to automatically start upon reboot.
现在让我们启动 ssh-agent 服务并将其配置为在重新启动时自动启动。
Open PowerShell as administrator and execute:以管理员身份打开PowerShell并执行:
Get-Service ssh-agent | Set-Service -StartupType Automatic -PassThru | Start-Service
Cd to your
$home\\.ssh dir
and executessh-add .\\<private-key-file>
to add your private key file (and passphrase) to ssh-agent.cd 到您的
$home\\.ssh dir
并执行ssh-add .\\<private-key-file>
将您的私钥文件(和密码)添加到 ssh-agent。
Last step is to get Git for Windows to use the WIndows nativessh.exe
.最后一步是让 Git for Windows 使用
ssh.exe
本机ssh.exe
。
To do that, execute:git config --global core.sshCommand C:/WINDOWS/System32/OpenSSH/ssh.exe
.为此,请执行:
git config --global core.sshCommand C:/WINDOWS/System32/OpenSSH/ssh.exe
。That's it!
就是这样! Now ssh-agent is running and serving your passphrase.
现在 ssh-agent 正在运行并为您的密码提供服务。 It will continue to do so after a reboot (without having to re-enter your passphrase).
重新启动后它将继续这样做(无需重新输入密码)。
And even better, ssh-agent will work for apps (like Visual Studio) that you start from the Start menu instead of the PowerShell console - which you would have to do it you usedstart-ssh-agent.cmd
.更好的是,ssh-agent 将适用于您从“开始”菜单而不是 PowerShell 控制台启动的应用程序(如 Visual Studio) - 您必须使用
start-ssh-agent.cmd
来执行此start-ssh-agent.cmd
。
Note:笔记:
I typed "
Services
" in theStart Menu
and I got a window listing all the services currently installed on the machine.我在“
Start Menu
输入“Services
”,然后出现一个窗口,列出了机器上当前安装的所有服务。
I noticed that, next to "OpenSSH Authentication Agent
", it said "Stopped
".我注意到,在“
OpenSSH Authentication Agent
”旁边,它显示“已Stopped
”。I ran
Get-Service ssh-agent | Set-Service -StartupType Manual -PassThru
我运行了
Get-Service ssh-agent | Set-Service -StartupType Manual -PassThru
Get-Service ssh-agent | Set-Service -StartupType Manual -PassThru
and thenStart-Service ssh-agent
and it worked.Get-Service ssh-agent | Set-Service -StartupType Manual -PassThru
然后Start-Service ssh-agent
就可以了。
It seems like the service was disabled or something.似乎该服务被禁用或什么的。 I've never touched any of these things on Windows, maybe it comes as "Stopped" by default.
我从来没有在 Windows 上接触过这些东西,也许默认情况下它是“停止”的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.