如何为我的密钥存储密码,以便在 Windows 上使用 git 时可以提交和从存储库中提取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3625148/
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 09:01:22  来源:igfitidea点击:

How do I store a password for my key so I can commit and pull from repository when using git on windows?

gitgithub

提问by Richard

Can someone point me in the direction I need look so I can configure my GIT client with the password needed for my private key? Every time I push and pull from my repository it asks me for the password for my key. I use command line and have the windows GIT client installed to use ssh.

有人可以指出我需要查看的方向,以便我可以使用私钥所需的密码配置我的 GIT 客户端吗?每次我从我的存储库中推送和拉取时,它都会询问我的密钥密码。我使用命令行并安装了 Windows GIT 客户端以使用 ssh。

Thanks for any pointers.

感谢您的指点。

采纳答案by Peter Tillemans

You have to recreate your default ssh key and use an empty passphrase. Then upload the public part again to the git server.

您必须重新创建默认的 ssh 密钥并使用空密码。然后再次上传公共部分到git服务器。

Without specific products you use on client and server it is a bit difficult to be more specific.

如果没有您在客户端和服务器上使用的特定产品,则更具体一些有点困难。

An alternative is to use ssh-agent, but I have no clue if this also works on Windows or if something similar is available.

另一种方法是使用 ssh-agent,但我不知道这是否也适用于 Windows,或者是否有类似的东西可用。

回答by Guilherme Garnier

I just had this problem here. My local git tree was corrupted, so I deleted it and cloned the project again from github. After that, it started to ask for my password on every pull or push. After some time, I realized I cloned the project with the HTTP URL (https://[email protected]/user/project.git). When I cloned again using the SSH URL ([email protected]:user/project.git), it stoped asking for the password.

我刚刚在这里遇到了这个问题。我本地的 git 树损坏了,所以我把它删除了,然后从 github 上重新克隆了这个项目。之后,它开始在每次拉或推时询问我的密码。一段时间后,我意识到我使用 HTTP URL (https://[email protected]/user/project.git) 克隆了项目。当我使用 SSH URL ([email protected]:user/project.git) 再次克隆时,它停止询问密码。

回答by Greg Bacon

Are you sure you're cloning over SSH?

您确定要通过 SSH 进行克隆吗?

Run git remote -v. The output below is from a repository that clones via HTTPS: notice the https://URL scheme.

运行git remote -v。下面的输出来自通过 HTTPS 克隆的存储库:注意https://URL 方案。

origin  https://github.com/git/git.git (fetch)
origin  https://github.com/git/git.git (push)

Clone URLs for SSH will have one of two forms:

SSH 的克隆 URL 将具有以下两种形式之一:

Assuming your host supports both SSH and HTTPS (as GitHub, GitLab, and Gitorious do), then simply change the remote's URL rather than recloning the entire history.

假设您的主机同时支持 SSH 和 HTTPS(如 GitHub、GitLab 和 Gitorious 所做的那样),那么只需更改远程的 URL,而不是重新克隆整个历史记录。

git remote set-url origin [email protected]:foo/bar/baz.git

Different hosts will have different URL designs. For example, switch the above GitHub URL from HTTPS to SSH with

不同的主机会有不同的 URL 设计。例如,将上面的 GitHub URL 从 HTTPS 切换到 SSH

git remote set-url [email protected]:git/git.git

GitHub has a related guide for changing a git remote's URL.

GitHub 有一个更改 git 远程 URL的相关指南

Windows

视窗

On Windows, a typical setup uses PuTTYas the SSH client, which means you'll want to run Pageant, PuTTY's SSH agent. On Windows, I run a quick batch job out of the Startup group:

在 Windows 上,典型设置使用PuTTY作为 SSH 客户端,这意味着您需要运行Pageant,PuTTY 的 SSH 代理。在 Windows 上,我从启动组中运行了一个快速批处理作业:

@echo off
start /b "C:\Program Files\PuTTY\pageant.exe" "C:\Users\Greg\Greg.ppk"

where Greg.ppkis a key that I created with PuTTYgen.

Greg.ppk我用PuTTYgen创建的密钥在哪里。

One more step: tell git to use plink, PuTTY's client for non-interactive connections. Set the environment variable GIT_SSHto

另一个步骤:告诉 git 使用plink,PuTTY 的客户端进行非交互式连接。将环境变量设置GIT_SSH

C:\Program Files\PuTTY\plink.exe

assuming that's where PuTTY lives.

假设那是 PuTTY 居住的地方。

Unix, Linux, Cygwin, Git Bash

Unix、Linux、Cygwin、Git Bash

On Unix-like platforms, the fix is straightforward. If ssh-agentisn't running, start it with, for example

在类 Unix 平台上,修复很简单。如果ssh-agent没有运行,启动它,例如

$ eval `ssh-agent`

and then add your default identity with

然后添加您的默认身份

$ ssh-add

If you have an identity somewhere else, run

如果您在其他地方有身份,请运行

$ ssh-add /path/to/other/ssh_id

If you're still having trouble, GitHub has a page for troubleshooting issues with GitHub and SSH, but please also update your question so we can make this a more helpful resource.

如果您仍然遇到问题,GitHub 有一个页面可以解决 GitHub 和 SSH 的问题,但也请更新您的问题,以便我们将其变成更有用的资源。

Downside of a passwordless SSH key

无密码 SSH 密钥的缺点

It may seem tempting to create and register an SSH key that has no password or passphrase because you won't be prompted for it with each git clone, git fetch, git pull, or git push. However, with no passphrase, your SSH key is unencrypted. This means that anyone who is able to obtain a copy of your private key (the file named id_rsa, id_dsa, id_ecdsa— without the .pubextension) can impersonate you with no effort.

它可能看起来很诱人创建和注册没有密码或口令,因为你不会被提示输入其与每个SSH密钥git clonegit fetchgit pull,或git push。但是,如果没有密码短语,您的 SSH 密钥是未加密的。这意味着任何能够获得您的私钥副本(名为id_rsa, id_dsa, id_ecdsa— 不带.pub扩展名的文件)的任何人都可以毫不费力地冒充您。

The nice design of SSH means that it is possible to make operations bothconvenient and secure. Go ahead and make your setup secure today. For a few minutes of extra effort now, you won't have to wonder about it down the road.

SSH 的漂亮设计意味着可以使操作方便又安全。继续并确保您的设置安全。现在只需几分钟的额外努力,您就不必再为它担心了。

If you've already set up a passwordless key, encrypt it by running

如果您已经设置了无密码密钥,请通过运行对其进行加密

ssh-keygen -p

You will see prompts for the key file (with a default value), for your new passphrase, and again to confirm that you typed your passphrase correctly.

您将看到密钥文件(带有默认值)、新密码的提示,并再次确认您输入的密码正确。

回答by marshally

The magic words that you are looking for is "ssh-agent", which is a way of automatically entering the passphrase for an ssh key.

您正在寻找的神奇词是ssh-agent,这是一种自动输入 ssh 密钥密码的方法。

This article from Githubgives the lowdown on how to get an ssh-agent running using the msysgit Windows client.

Github 上的这篇文章提供了有关如何使用 msysgit Windows 客户端运行 ssh-agent 的详细信息。

Git on Windows is a difficult experience. You will spend your days wandering the internet finding articles that playfully suggest a series of keypresses that only work properly on Unix.

Windows 上的 Git 是一种艰难的体验。您将花费大量时间在互联网上寻找文章,这些文章俏皮地建议了一系列只能在 Unix 上正常工作的按键。

I'm not saying this to taunt you. Just to prepare you for the fact that this is going to be difficult. In many ways, you'll be better off setting up a Unix VM for whatever it is you are trying to do.

我说这些不是为了嘲讽你。只是为了让您为这将是困难的事实做好准备。在许多方面,您最好为您尝试做的任何事情设置一个 Unix VM。

That being said, here are a couple of helpful links:

话虽如此,这里有几个有用的链接:

GitHub's Installing Git on Windowsseries GitHub's Generating SSH Keys on WindowsAnd some general notes about passphrases

GitHub在 Windows安装 Git系列 GitHub在 Windows生成 SSH 密钥以及一些关于密码的一般注意事项

If you don't find what you are looking for on Windows, always check the Github docs first. As far as I have been able to tell, they are the only Windows guides worth a damn.

如果您在 Windows 上找不到您要查找的内容,请务必先查看 Github 文档。据我所知,它们是唯一值得一看的 Windows 指南。