有没有办法缓存 https 凭据以推送提交? [英] Is there a way to cache https credentials for pushing commits?

查看:25
本文介绍了有没有办法缓存 https 凭据以推送提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近切换到将我的存储库同步到 GitHub 上的 https://(由于防火墙问题),它每次都要求输入密码.

I recently switched to synchronizing my repositories to https:// on GitHub (due to firewall issues), and it asks for a password every time.

有没有办法缓存凭证,而不是每次 git push 时都进行身份验证?

Is there a way to cache the credentials, instead of authenticating every time that git push?

推荐答案

使用 Git 1.7.9 及更高版本

自 Git 1.7.9(于 2012 年 1 月下旬发布)以来,Git 中有一种巧妙的机制可以避免在 HTTP/HTTPS 时一直输入密码,称为 凭证助手.(感谢 dazonic 在下面的评论中指出这个新功能.)

With Git version 1.7.9 and later

Since Git 1.7.9 (released in late January 2012), there is a neat mechanism in Git to avoid having to type your password all the time for HTTP / HTTPS, called credential helpers. (Thanks to dazonic for pointing out this new feature in the comments below.)

对于 Git 1.7.9 或更高版本,您可以只使用以下凭证助手之一:

With Git 1.7.9 or later, you can just use one of the following credential helpers:

git config --global credential.helper cache

credential.helper 缓存值 告诉 Git 将您的密码缓存在特定分钟的内存.默认为 15 分钟,您可以设置更长的超时时间:

The credential.helper cache value tells Git to keep your password cached in memory for a particular amount of minutes. The default is 15 minutes, you can set a longer timeout with:

git config --global credential.helper "cache --timeout=3600"

将缓存设置为 1 小时,或者:

Which sets the cache for 1 hour, or:

git config --global credential.helper "cache --timeout=86400"

1 天.如果需要,您还可以永久存储您的凭据,请参阅下面的其他答案.

For 1 day. You can also store your credentials permanently if so desired, see the other answers below.

GitHub 的帮助 还建议,如果您在Mac OS X 并使用 Homebrew 安装 Git,可以使用原生 Mac OSX 密钥库:

GitHub's help also suggests that if you're on Mac OS X and used Homebrew to install Git, you can use the native Mac OS X keystore with:

git config --global credential.helper osxkeychain

对于 Windows,有一个名为 Git 的助手适用于 Windows 的凭据管理器msysgit 中的 wincred.

For Windows, there is a helper called Git Credential Manager for Windows or wincred in msysgit.

git config --global credential.helper wincred # obsolete

使用 Git for Windows 2.7.3+(2016 年 3 月):

With Git for Windows 2.7.3+ (March 2016):

git config --global credential.helper manager

对于 Linux,您将使用(2011 年)gnome-keyring(或其他密钥环实现,如 KWallet).

For Linux, you would use (in 2011) gnome-keyring(or other keyring implementation such as KWallet).

现在(2020 年),那将是(在 Linux 上)

Nowadays (2020), that would be (on Linux)

sudo dnf install git-credential-libsecret
git config --global credential.helper /usr/libexec/git-core/git-credential-libsecret

Ubuntu

sudo apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret


使用 1.7.9 之前的 Git 版本

对于 1.7.9 之前的 Git 版本,此更安全的选项不可用,您需要更改 origin 远程使用的 URL 以这种方式包含密码:


With Git versions before 1.7.9

With versions of Git before 1.7.9, this more secure option is not available, and you'll need to change the URL that your origin remote uses to include the password in this fashion:

https://you:password@github.com/you/example.git

... 换句话说,在用户名之后和 @ 之前使用 :password.

... in other words with :password after the username and before the @.

你可以为你的 origin 遥控器设置一个新的 URL:

You can set a new URL for your origin remote with:

git config remote.origin.url https://you:password@github.com/you/example.git

请确保您使用的是 https,并且您应该知道,如果您这样做,您的 GitHub 密码将以明文形式存储在您的 .git 目录中,其中显然是不可取的.

Make sure that you use https, and you should be aware that if you do this, your GitHub password will be stored in plaintext in your .git directory, which is obviously undesirable.

另一种方法是将您的用户名和密码放在 ~/.netrc 文件中,尽管将密码保存在远程 URL 中,这意味着您的密码将存储在纯文本格式的磁盘,因此安全性较低,不推荐使用.但是,如果您想采用这种方法,请将以下行添加到您的 ~/.netrc:

An alternative approach is to put your username and password in your ~/.netrc file, although, as with keeping the password in the remote URL, this means that your password will be stored on the disk in plain text and is thus less secure and not recommended. However, if you want to take this approach, add the following line to your ~/.netrc:

machine <hostname> login <username> password <password>

... 将 替换为服务器的主机名,将 替换为您的用户名和密码.还要记住为该文件设置限制性文件系统权限:

... replacing <hostname> with the server's hostname, and <username> and <password> with your username and password. Also remember to set restrictive file system permissions on that file:

chmod 600 ~/.netrc

请注意,在 Windows 上,此文件应命名为 _netrc,您可能需要定义 %HOME% 环境变量 - 有关详细信息,请参阅:

Note that on Windows, this file should be called _netrc, and you may need to define the %HOME% environment variable - for more details see:

这篇关于有没有办法缓存 https 凭据以推送提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆