有没有一种方法可以缓存GitHub凭证来推送提交? [英] Is there a way to cache GitHub credentials for pushing commits?

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

问题描述

我最近切换到将存储库同步到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

...,它告诉Git将您的密码在内存中缓存15分钟(默认情况下).您可以通过以下方式设置更长的超时时间:

... which tells Git to keep your password cached in memory for (by default) 15 minutes. You can set a longer timeout with:

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

(该示例在 GitHub帮助页面中进行了建议Linux .)如果需要,您也可以永久存储凭据,请参见下面的其他答案.

(That example was suggested in the GitHub help page for Linux.) You can also store your credentials permanently if so desired, see the other answers below.

GitHub的帮助也建议 Mac OS X,并使用 Homebrew 来安装Git,则可以使用本机Mac  OS&X密钥库,具有:

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,有一个名为用于Windows的Git凭据管理器的助手.在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

使用适用于Windows的Git 2.7.3+ (2016年3月):

With Git for Windows 2.7.3+ (March 2016):

git config --global credential.helper manager

对于Linux,您可以使用gnome-keyring (或其他密钥环实现,例如KWallet).

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

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

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>

...用服务器的主机名替换<hostname>,用您的用户名和密码替换<username><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:

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

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