在不使用 bash 的情况下在 Windows 7 上将 git 连接到 github

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2328599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:55:16  来源:igfitidea点击:

Connecting git to github on windows 7 without bash

gitpowershellwindows-7sshgithub

提问by George Mauer

I'm setting up git on my new Windows 7 machine and I'm hitting a roadblock when it comes to getting github to acknowledge my ssh key. I am doing things a little different from the standard script in that I would rather not use cygwin and prefer to use my powershell prompt. The following is what I did:

我正在我的新 Windows 7 机器上设置 git,但在让 github 确认我的 ssh 密钥时遇到了障碍。我做的事情与标准脚本有点不同,因为我宁愿不使用 cygwin 而更喜欢使用我的 powershell 提示符。以下是我所做的:

  1. I installed msysgit (portable).
  2. I went to C:\program files\git\bin and used ssh-keygen to generate a public/private ssh keypair which I put in c:\Temp
  3. I then created a directory named .ssh\ in c:\Users\myusername\ (on windows 7)
  4. I moved both the files generated by the ssh-keygen (id_rsa and id_rsa.pub) into the .ssh directory
  5. I went to my account on github, created a new public key, copy-pasted the contents of id_rsa.pub into it and saved
  6. I now go to my powershell prompt, set-alias git 'C:\program files\git\bin\git.exe'
  7. I try to now do a clone [email protected]:togakangaroo/ps-profile.git which rejects my authentication:

    Permission denied (publickey). fatal: The remote end hung up unexpectedly

  1. 我安装了 msysgit(便携式)。
  2. 我去了 C:\program files\git\bin 并使用 ssh-keygen 生成一个公共/私有 ssh 密钥对,我把它放在 c:\Temp
  3. 然后我在 c:\Users\myusername\(在 Windows 7 上)创建了一个名为 .ssh\ 的目录
  4. 我将 ssh-keygen 生成的文件(id_rsa 和 id_rsa.pub)移动到 .ssh 目录中
  5. 我去了我在github上的帐户,创建了一个新的公钥,将id_rsa.pub的内容复制粘贴到其中并保存
  6. 我现在转到我的 powershell 提示符,设置别名 git 'C:\program files\git\bin\git.exe'
  7. 我现在尝试克隆 [email protected]:togakangaroo/ps-profile.git 拒绝我的身份验证:

    权限被拒绝(公钥)。致命:远端意外挂断

Past experience says that this means git is not recognizing my key. What steps am I missing?

过去的经验表明,这意味着 git 无法识别我的密钥。我缺少哪些步骤?

I have a feeling that I need to somehow configure git so that it knows where my ssh keys are (though it would seem it should look there automatically) but I don't know how to do that.

我有一种感觉,我需要以某种方式配置 git,以便它知道我的 ssh 密钥在哪里(尽管它似乎应该自动查找),但我不知道该怎么做。

Another possible clue is that when I try to run git config --global user.name "George Mauer" I get an error

另一个可能的线索是,当我尝试运行 git config --global user.name "George Mauer" 时出现错误

fatal: $HOME not set

I did however set up a HOME environment user variable with the value %HOMEDRIVE%%HOMEPATH%

但是,我确实设置了一个值为 %HOMEDRIVE%%HOMEPATH% 的 HOME 环境用户变量

采纳答案by Nick Novitski

The command you're looking for is: ssh-add C:\path\to\key

您正在寻找的命令是: ssh-add C:\path\to\key

First, you may want to find out where ssh is currently looking for your keys, by running ssh -v [email protected]

首先,您可能想通过运行来找出 ssh 当前在哪里寻找您的密钥 ssh -v [email protected]

回答by Christian Deger

I had the same problem. I accidently added the wrong directory to the path.

我有同样的问题。我不小心在路径中添加了错误的目录。

After I changed that from *\Git\bin\to *\Git\cmd\everything worked.

在我将其更改*\Git\bin\*\Git\cmd\一切正常之后。

git.cmdsets up the environment variables.

git.cmd设置环境变量。

回答by Micah Stubbs

My solution to the

我的解决方案

"Could not open a connection to your authentication agent."

“无法打开与您的身份验证代理的连接。”

error on Windows 7 was to first run ssh -v [email protected]in the git Bash console to find out what sort of public key file Github was looking for. The filename I saw was id_rsa.

Windows 7 上的错误是首先ssh -v [email protected]在 git Bash 控制台中运行以找出 Github 正在寻找什么样的公钥文件。我看到的文件名是id_rsa.

Then, I viewed the c:\users\username\.sshfolder in Windows Explorer and noticed that while there was no id_rsafile, there was github_rsaand github_rsa.pub.

然后,我看到的c:\users\username\.ssh文件夹在Windows资源管理器,发现虽然没有id_rsa文件,有github_rsagithub_rsa.pub

Copying these files in the c:\users\username\.sshfolder and renaming the copies to id_rsaand id_rsa.pubrespectively solved the issue for me.

将这些文件复制到c:\users\username\.ssh文件夹中并将副本重命名为id_rsaid_rsa.pub分别为我解决了问题。

I was then able to push local commits to the repository on Github.

然后我能够将本地提交推送到 Github 上的存储库。

回答by rahmat

I have same problem, It's fixed by putting your id_rsa* file at C:\Program Files\Git\.sshfolder

我有同样的问题,它通过将您的id_rsa* 文件放在C:\Program Files\Git\.ssh文件夹中来解决

回答by pulo71

I had this problem on Windows 7. Once I addressed the missing HOME environment variable by pointing HOME to c/Users/wherever the problem was fixed.

我在 Windows 7 上遇到了这个问题。一旦我通过将 HOME 指向 c/Users/ 问题解决的地方来解决丢失的 HOME 环境变量。

More here: http://code.google.com/p/msysgit/issues/detail?id=482

更多信息:http: //code.google.com/p/msysgit/issues/detail?id=482

回答by UtsavShah

I had the same problem with PowerShell for ~3 hours.

我在 PowerShell 上遇到了同样的问题约 3 个小时。

Simply set your HOME variable to be

只需将您的 HOME 变量设置为

C:\Users\Your Username

and restart PowerShell. Then ensure all your keys and all match.

并重新启动 PowerShell。然后确保您的所有密钥都匹配。

回答by iainjames9

Unless it's completely necessary, I would recommend not using the portable mysysgit. This worked almost out-of-the-box for me using the normal mysysgit installed.

除非完全有必要,否则我建议不要使用便携式 mysysgit。使用安装的普通 mysysgit,这对我来说几乎是开箱即用的。

However, I did recently set this up on a new Windows machine and ran into a similar issue. This was fixed by converting my plink SSH key into an OpenSSH-compatible key, and moving this into my personal .ssh folder. After that, it worked a charm!

但是,我最近确实在一台新的 Windows 机器上设置了它并遇到了类似的问题。这是通过将我的 plink SSH 密钥转换为与 OpenSSH 兼容的密钥并将其移动到我的个人 .ssh 文件夹中来解决的。在那之后,它发挥了魅力!

If interested, see here

如果有兴趣,请看这里

回答by Simon

I had the problem, but it was caused by Internet Explorer 8.0.7600 having a JavaScript error when trying to perform the "add key" button functionality on my GitHub login "Account Settings" page "SSH Public Keys". This meant that the add key failed, and the key was not listed on the page of public keys above the link "Add another key". I switched to Firefox.

我遇到了这个问题,但它是由 Internet Explorer 8.0.7600 在尝试在我的 GitHub 登录“帐户设置”页面“SSH 公钥”上执行“添加密钥”按钮功能时出现 JavaScript 错误造成的。这意味着添加密钥失败,并且密钥未列在链接“添加另一个密钥”上方的公钥页面上。我切换到 Firefox。