如何让 Git 在推送到远程存储库时只询问密码

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

How to make Git only ask password when push to remote repository

gitgithubproxyusername

提问by EthanZ

The requirement is probably odd. But sometimes I really need this functionality. Ok, here is the thing, I'm always working behind proxies. Either at office I'm using corp proxy or at home I'm using VPN and proxy as well (You know, I'm in China, so I hope you can understand).

这个要求可能很奇怪。但有时我真的需要这个功能。好的,事情就是这样,我总是在代理后面工作。无论是在办公室我使用公司代理还是在家里我也在使用 VPN 和代理(你知道,我在 CN ,所以我希望你能理解)。

Usually I'm using git protocol to connect github remote repository, but it's hard for me to set proxy behind git protocol, so I decided to switch back to http(s) with following command

通常我使用git协议连接github远程存储库,但是我很难在git协议后面设置代理,所以我决定使用以下命令切换回http(s)

git remote set-url origin https://github.com/<username>/repo.git

git remote set-url origin https://github.com/<username>/repo.git

git config http.proxy http://proxy:8080

git config http.proxy http://proxy:8080

And it works like a charm. But git asks for usernameand passwordwhenever I connect to the remote server, for example pull/push.

它就像一个魅力。但是每当我连接到远程服务器时,git 都会要求输入用户名密码,例如 pull/push。

I only have one github account and I want to save some typing, so the question is how to avoid typing github username everytime I want to push. I could accept to type my password, but I don't want to type username. How to achieve that?

我只有一个 github 帐户,我想保存一些输入,所以问题是如何避免每次我想推送时都输入 github 用户名。我可以接受输入我的密码,但我不想输入用户名。如何做到这一点?

回答by Michael Aquilina

Store Password

存储密码

You could tell git to store your credentials using the following command:

您可以使用以下命令告诉 git 存储您的凭据:

git config --global credential.helper store

Using this method, you only need to enter your username and password onceand git will never ask for it again. But please note that your password will be stored in plaintextwhich is not great in terms of security.

使用这种方法,你只需要输入一次用户名和密码,git 就不会再要求了。但请注意,您的密码将以明文形式存储,这在安全性方面不是很好。

Cache Password

缓存密码

You can also go for caching instead which will store your password after having typed it once in a session for some period of time.

您也可以改为使用缓存,在会话中输入一次密码一段时间后,它会存储您的密码。

git config --global credential.helper cache

This is more secure as your password won't be stored on disk - just temporarily in memory. You can set the timeout yourself if your not happy with the default:

这更安全,因为您的密码不会存储在磁盘上 - 只是暂时存储在内存中。如果您对默认设置不满意,您可以自己设置超时时间:

git config --global credential.helper 'cache --timeout=600'

Once again this is not always ideal.

再一次,这并不总是理想的。

SSH Agent - The Ideal Solution

SSH 代理 - 理想的解决方案

What you should really be using is the sshprotocol to push and pull your data. This will allow you to use your private ssh key to authenticate yourself - which will be handled by your operating system's installed key agent. This should work with proxies without any issues so you should definitely give it a go.

您真正应该使用的是ssh推送和拉取数据的协议。这将允许您使用您的私人 ssh 密钥来验证您自己——这将由您的操作系统安装的密钥代理处理。这应该可以在没有任何问题的情况下与代理一起使用,因此您绝对应该试一试。

You can set it up by setting your remote url as follows:

您可以通过设置远程 url 来设置它,如下所示:

git remote set-url origin [email protected]:<username>/<project>.git

If you are using another hosting service like bitbucket, just replace "github.com" with your providers domain.

如果您使用其他托管服务,如 bitbucket,只需将“github.com”替换为您的提供商域。

Once you do that, you will need to set up a public and private key pair for communication between github and your computer. There is a very good tutorial on how to set it up here. If you are using Linux or MacOSX you simply need to follow the steps when running the command ssh-keygen.

完成此操作后,您将需要设置公钥和私钥对,以便在 github 和您的计算机之间进行通信。有关于如何设置了一个很好的教程在这里。如果您使用的是 Linux 或 MacOSX,您只需在运行命令时按照以下步骤操作即可ssh-keygen

After that, you can get an SSH agent to store your password for you which is typically more secure. SSH agents usually ask you to input your password just once after turning on your computer - but after that it should do everything automatically for you. Passwords are stored securely this way unlike the first solution which stores passwords in plain text.

之后,您可以让 SSH 代理为您存储密码,这通常更安全。SSH 代理通常会要求您在打开计算机后只输入一次密码 - 但之后它应该会自动为您执行所有操作。与以纯文本形式存储密码的第一种解决方案不同,以这种方式安全地存储密码。

The SSH agent you use will depend on your operating system but it shouldn't be hard to set up. With most popular Linux distros you shouldn't need to do anything and I have been informed in a comment below that you can set Windows up as follows:

您使用的 SSH 代理将取决于您的操作系统,但设置起来应该不难。对于大多数流行的 Linux 发行版,您不需要做任何事情,我在下面的评论中获悉您可以按如下方式设置 Windows:

git config --global credential.helper wincred

I have never used MacOSX so I cannot say for certain whether this is automatically set up like it is in Ubuntu based distributions.

我从未使用过 MacOSX,所以我不能肯定地说这是否像在基于 Ubuntu 的发行版中一样自动设置。

回答by VonC

To complete Nevik's comment, you can change the url of an existing repo:

要完成Nevik评论,您可以更改现有存储库的 url:

git remote set-url origin https://<username>@github.com/<username>/repo.git 

That will specify the username the proxy authentication needs, leaving for you to enter the password.

这将指定代理身份验证所需的用户名,让您输入密码。

I personally store all my different credentials (GitHub, BitBucket, ...) in an encrypted %HOME%\_netrc.gpg(Windows) or ~/.netrc.gpg(Unix) using the _netrccredential helper (git1.8.3+), rather than the memory cache credential helper described in Michael's answer.

我个人使用凭证助手 (git1.8.3+)将所有不同的凭证(GitHub、BitBucket 等)存储在加密的%HOME%\_netrc.gpg(Windows) 或~/.netrc.gpg(Unix _netrc) 中,而不是Michael回答中描述的内存缓存凭证助手.

See more at "Configure Git clients, like GitHub for Windows, to not ask for authentication".

请参阅“配置 Git 客户端,如 Windows 版 GitHub,不要求进行身份验证”。

The difference is:

区别在于:

  • with the memory cache credential helper, you have to remember your GitHub password
  • with a gpg netrc credential helper, you only have to remember your gpg passphrase, for allyour different credentials.
  • 使用内存缓存凭据助手,您必须记住您的 GitHub 密码
  • 使用 gpg netrc 凭证助手,您只需要记住您的 gpg 密码,用于所有不同的凭证。

Plus, if you activated GitHub two-factor authentication, you can no longer use your regular GitGub password, but you need a "Personal Access Token", which is a 40-lenght random string. One more reason to store that once and for all in an encrypted file, rather than having to enter it once per session.

另外,如果您激活了GitHub 双重身份验证,您将不能再使用您的常规 GitGub 密码,但您需要一个“个人访问令牌”,它是一个 40 长度的随机字符串。将其一劳永逸地存储在加密文件中的另一个原因,而不是每次会话都必须输入一次。

What I just described is for https url, which, in my opinion, remains easier to manage than ssh url, with their public/private ssh keys that you need to passphrase protect, andthat you need to publish on your GitHub account (one different per workstation, since you shouldn't be reusing ssh keys)

我刚刚描述的是 https url,在我看来,它仍然比 ssh url 更易于管理,使用它们的公共/私有 ssh 密钥需要密码保护,并且您需要在您的 GitHub 帐户上发布(一个不同的每个工作站,因为您不应该重复使用 ssh 密钥)

回答by tharakaucsc

This is the simple solution for saving some typing you can use the following steps in git bash easily..

这是保存一些输入的简单解决方案,您可以轻松地在 git bash 中使用以下步骤..

(1) create the remote repository

(1) 创建远程仓库

git remote add origin https://{your_username}:{your_password}@github.com/{your_username}/repo.git

Note: If your password contains '@' sign use '%40' instead of that

注意:如果您的密码包含 '@' 符号,请使用 '%40' 而不是那个

(2) Then do anything you want with the remote repository

(2) 然后对远程仓库做任何你想做的事情

ex:- git push origin master