gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:33:10 24 4
gpt4 key购买 nike

我在 Windows 10 上安装了 Git for Windows。我正在将代码推送到两个不同的 GitHub 帐户,并且我正在尝试将我的 SSH 代理设置为使用两个 key (每个帐户一个)。

这是我的 ~\.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

如果我使用 Git Bash 推/pull ,它可以正常工作,但我也希望能够使用 Powershell。当我尝试从 Powershell 执行推送或 pull 操作时,出现“请确保您具有正确的访问权限”错误。如果我从 Powershell 运行 start-ssh-agent.cmd,则 git push 适用于该 Powershell 实例。

如果我将 start-ssh-agent.cmd 添加到我的 profile.ps1 配置文件中,SSH 代理就会启动并且 git push 会正常工作,但这似乎破坏了我的 Powershell 配置文件的其他方面(如下所示)。 Import-ModuleSet-Theme 不再有效。我没有收到任何错误;我只是不再得到我的 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

我找到的大部分指令都涉及运行 eval $(ssh-agent -s),但是 eval 在电源外壳。

我该如何设置才能使用 Powershell 从两个不同的 GitHub 帐户推送/pull 数据?

最佳答案

首先,只有当您在创建私钥时使用密码保护了这些私钥时,您才需要 ssh-agent。

其次,确保使用正确的 SSH URL 形式,引用那些 ~/.ssh/config 条目:

github.com-usertwo:<usertwo>/myRepo

dahlbyk/posh-git issue 640 中所述

Rather than use Git's start-ssh-agent.cmd, I would use the built-in OpenSSH you have on Windows 10 1803.
To use this follow these steps:

From a PowerShell window run get-command ssh.exe. You should see that ssh.exe is located at 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. Once that is open select OpenSSH Client to add that feature. After that has finished, open PowerShell again and see if get-command ssh.exe returns C:\Windows\System\OpenSSH\ssh.exe

Now let's start the ssh-agent service and configure it to automatically start upon reboot.
Open PowerShell as administrator and execute:

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.
Last step is to get Git for Windows to use the WIndows native ssh.exe.
To do that, execute: git config --global core.sshCommand C:/WINDOWS/System32/OpenSSH/ssh.exe.

That's it! Now ssh-agent is running and serving your passphrase. 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.

注意:

I typed "Services" in the Start Menu and I got a window listing all the services currently installed on the machine.
I noticed that, next to "OpenSSH Authentication Agent", it said "Stopped".

I ran Get-Service ssh-agent | Set-Service -StartupType Manual -PassThru and then Start-Service ssh-agent and it worked.
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.

关于git - 如何在 Powershell 中使用 SSH key 推送到两个不同的 GitHub 帐户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60696613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com