简体   繁体   English

如何在 Powershell 中使用 SSH 密钥推送到两个不同的 GitHub 帐户?

[英]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-ModuleSet-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 640dahlbyk/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 that ssh.exe is located at C:\\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 returns C:\\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 execute ssh-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 native ssh.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 used start-ssh-agent.cmd .更好的是,ssh-agent 将适用于您从“开始”菜单而不是 PowerShell 控制台启动的应用程序(如 Visual Studio) - 您必须使用start-ssh-agent.cmd来执行此start-ssh-agent.cmd

Note:笔记:

I typed " Services " in the Start 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 then Start-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.

相关问题 如何使用TortoiseGit为两个GitLab帐户设置两个SSH密钥并推/拉? - How to set up two SSH keys for two GitLab accounts and push/pull by using TortoiseGit? 我必须采取什么步骤才能使用SSH持续推送到两个Github帐户? - What steps must I take to consistently push to two Github accounts using SSH? 如何在 SSH 和 Red Hat 7 中使用两个不同的 github 帐户? - How to use two different github accounts with SSH and Red Hat 7? 如何使用 git 从我的 PC 推送到多个 GitHub 帐户? (不同的存储库) - How can I push to multiple GitHub accounts from my PC using git? (different repositories) Github - 对于不同的项目,我是否需要多个 SSH 密钥? (苹果系统) - Github - Do I need multiple SSH keys for different projects? (MacOS) 使用多个 ssh 密钥/github 帐户时获取“未找到存储库” - Getting 'repository not found' when using multiple ssh keys / github accounts 如何在 GitHub 上使用多个 SSH 密钥? - How do I use multiple SSH keys on GitHub? 如何使用 Ubuntu (systemd)、Github 和 SSH 密钥在启动时从 git 中提取 - How do I pull from git on boot using Ubuntu (systemd), Github and SSH keys 如何将一个本地 git 仓库推送到两个不同的 github 帐户 - How to push one single local git repo to two different github accounts 为此我需要两个 GitHub 帐户吗? - Do I need two GitHub accounts for this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM