我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),并且每次都要求输入密码。
有没有办法缓存凭据,而不是每次git push
验证?
自Git 1.7.9(2012年1月下旬发布)以来,Git中有一个简洁的机制,可以避免为HTTP / HTTPS一直输入密码,称为credential helpers。 (感谢dazonic在下面的评论中指出了这个新功能。)
使用Git 1.7.9或更高版本,您可以使用以下凭证助手之一:
git config --global credential.helper cache
...告诉Git将密码缓存在内存中(默认情况下)为15分钟。您可以设置更长的超时:
git config --global credential.helper "cache --timeout=3600"
(该示例在GitHub help page for Linux中提出。)如果需要,您还可以永久存储您的凭证,请参阅下面的其他答案。
GitHub的帮助also suggests,如果您使用的是Mac OS X并使用Homebrew安装Git,您可以使用本机Mac OS X密钥库:
git config --global credential.helper osxkeychain
对于Windows,有一个名为Git Credential Manager for Windows或wincred in msysgit的助手。
git config --global credential.helper wincred # obsolete
随着Git for Windows 2.7.3+(2016年3月):
git config --global credential.helper manager
对于Linux,您可以使用use gnome-keyring
(或其他密钥环实现,如KWallet)。
对于1.7.9之前的Git版本,这个更安全的选项不可用,您需要更改origin
远程用来以这种方式包含密码的URL:
https://you:[email protected]/you/example.git
...换句话说,在用户名之后和:password
之前使用@
。
您可以使用以下命令为origin
遥控器设置新的URL:
git config remote.origin.url https://you:[email protected]/you/example.git
确保你使用https
,你应该知道,如果你这样做,你的GitHub密码将以明文形式存储在你的.git
目录中,这显然是不可取的。
另一种方法是将您的用户名和密码放在~/.netrc
文件中,但是,与将密码保存在远程URL中一样,这意味着您的密码将以纯文本形式存储在磁盘上,因此不太安全,不建议使用。但是,如果您想采用这种方法,请将以下行添加到~/.netrc
:
machine <hostname> login <username> password <password>
...用服务器的主机名替换<hostname>
,用你的用户名和密码替换<username>
和<password>
。还记得在该文件上设置限制性文件系统权限:
chmod 600 ~/.netrc
请注意,在Windows上,此文件应称为_netrc
,您可能需要定义%HOME%环境变量 - 有关更多详细信息,请参阅:
对于Windows,您可以使用Git Credential Manager(GCM)插件。它目前由Microsoft维护。好处是它将密码保存在Windows凭据存储中,而不是纯文本。
该项目的releases page上有一个安装程序。这也将安装Git for Windows的官方版本,内置凭证管理器。它允许two-factor authentication用于GitHub(和其他服务器)。并具有用于初始登录的图形界面。
对于Cygwin用户(或已经使用官方Git for Windows的用户),您可能更喜欢手动安装。从releases page下载zip包。解压缩包,然后运行install.cmd
文件。这将安装到您的~/bin
文件夹。 (确保您的~/bin
目录位于PATH中。)然后使用此命令对其进行配置:
git config --global credential.helper manager
然后,当对任何服务器进行身份验证时,Git将运行git-credential-manager.exe
。
如果您不想像Mark所说的那样以明文形式存储密码,则可以使用不同的GitHub URL进行提取,而不是使用推送。在配置文件中,在[remote "origin"]
下:
url = git://github.com/you/projectName.git
pushurl = [email protected]:you/projectName.git
当你推送时它仍然会要求输入密码,但是至少对于开源项目来说,它不会在你获取时提出。
您可以创建自己的qazxsw poi(qazxsw poi),并使用与使用普通凭据相同的方式(在:personal API token)。例如:
OAuth
/settings/tokens
另一种方法是在git remote add fork https://[email protected]/foo/bar
git push fork
(Windows上的.netrc
)中配置您的用户/密码,例如:
~/.netrc
对于HTTPS,请添加额外的行:
_netrc
对于使用HTTPS时的machine github.com
login USERNAME
password PASSWORD
,您可以使用凭证助手告诉Git每次与GitHub交谈时都记住您的GitHub用户名和密码。
protocol https
(需要cache your GitHub password in Git),git config --global credential.helper osxkeychain
osxkeychain helper
有关:
git config --global credential.helper wincred
git config --global credential.helper cache
克隆存储库git config --global credential.helper 'cache --timeout=x'
后,您可以编辑x
并添加如下配置:
repo
然后你不会被要求再次使用repo/.git/config
和[user]
name = you_name
password = you_password
[credential]
helper = store
。
我知道这不是一个安全的解决方案,但有时你只需要一个简单的解决方案 - 无需安装任何其他东西。由于helper = store对我不起作用,我创建了一个虚拟助手:
创建一个脚本并将其放在用户bin文件夹中,此处命名为credfake,此脚本将提供您的用户名和密码:
username
让它可执行:
password
然后在git中配置它:
#!/bin/bash
while read line
do
echo "$line"
done < "/dev/stdin"
echo username=mahuser
echo password=MahSecret12345
(或仅使用--global用于一个回购)
并且 - voilá - git将使用此用户+密码。
应使用身份验证令牌而不是帐户密码。转到GitHub设置/应用程序,然后创建个人访问令牌。令牌的使用方式与使用密码的方式相同。
该令牌旨在允许用户不使用帐户密码进行项目工作。仅在执行管理工作时使用密码,例如创建新令牌或撤消旧令牌。
可以使用项目特定部署密钥来授予对单个项目存储库的访问权限,而不是授予用户对GitHub帐户的完整访问权限的令牌或密码。当您仍然可以使用普通凭据访问其他Git帐户或项目时,可以将Git项目配置为在以下步骤中使用此不同的密钥:
chmod u+x /home/mahuser/bin/credfake
,git config --global credential.helper /home/mahuser/bin/credfake
,也许是Host
,也许是IdentityFile
(虽然我认为你不需要它)。UserKnownHostsFile
User
。这里ssh -F /path/to/your/config $*
(起源)必须使用GIT_SSH=/path/to/your/wrapper
格式。最好使用凭据进行安全性,但您可以使用缓存保留一段时间:
git remote
您的凭据将保存3600秒。
如果你像我一样使用[email protected]:user/project.git
,情况会有所不同。由于我在其他地方找不到好的答案,我会在这里贴一个,以便我以后可以找到它。
如果您使用的是双因素身份验证,则指定用户名/密码甚至无法正常工作 - 您将获得拒绝访问权限。但是您可以使用应用程序访问令牌并使用Git的凭据帮助程序为您缓存该令牌。以下是相关链接:
git config --global credential.helper cache
git config credential.helper 'cache --timeout=3600'
(搜索标题为“如何为命令行Git工作?”的部分)我不记得我在哪里看到这个,但当你被问到你的用户名时 - 那就是你坚持使用应用程序访问令牌的地方。然后将密码留空。它适用于我的Mac。
通常你有一个远程URL,像这样的,
Setting up the command-line to work with 2-factor auth
如果您想在使用Credential caching时跳过用户名和密码,请尝试以下操作:
git remote -v
origin https://gitlab.com/username/Repo.git (fetch)
origin https://gitlab.com/username/Repo.git (push)
我刚刚向原点添加了相同的URL(包括用户详细信息,包括密码)。
注意:如果用户名是电子邮件ID,则不起作用。
git push
您还可以让Git使用以下内容永久存储您的凭据:
git config credential.helper store
注意:虽然这很方便,但Git会将您的凭据以明文形式存储在项目目录下的本地文件(.git-credentials)中(请参阅下面的“home”目录)。如果您不喜欢这样,请删除此文件并切换到使用缓存选项。
如果您希望Git每次需要连接到远程存储库时都要继续询问您的凭据,您可以运行以下命令:
git config --unset credential.helper
要将密码存储在.git-credentials
目录中的%HOME%
而不是项目目录中:使用--global
标志
git config --global credential.helper store
我从 git remote set-url origin https://username:[email protected]/username/Repo.git
得到了答案。就我而言,我的Windows安装中没有凭证缓存;我使用凭证存储。
使用凭证存储后,用户名/密码存储在[用户文件夹] / .git-credentials文件中。要删除用户名/密码,只需删除文件的内容即可。
这对我有用,我正在使用Windows 10
git remote -v
origin https://username:[email protected]/username/Repo.git (fetch)
origin https://username:[email protected]/username/Repo.git (push)
你可以阻止它使用GitHub API的作曲家文档gitcredentials(7) Manual Page,使它像git config --global credential.helper wincred
一样:
如果在GitHub存储库上将mentions密钥设置为
git clone
,它将像使用任何其他Git存储库而不是使用GitHub API一样克隆存储库。但与直接使用no-api
驱动程序不同,composer仍将尝试使用GitHub的zip文件。
所以该部分看起来像这样:
true
请记住,API存在是有原因的。因此,对于github.com上增加的负载,这应该是最后的手段。
您还可以编辑bashrc文件并在其中添加脚本。
这将在您启动Git时请求您的密码,然后在您注销之前记住它。
git
保存Git存储库的密码在Windows上使用~/.netrc
(Unix)或%HOME%/_netrc
(请注意_
)可以使用HTTPS URL。
但是:该文件将以纯文本格式存储您的密码。
解决方案:使用GPG (GNU Privacy Guard)加密该文件,并在每次需要密码时使Git解密(对于push
/ pull
/ fetch
/ clone
操作)。
注意:使用Git 2.18(2018年第二季度),您现在可以自定义用于解密加密的.netrc
文件的GPG。
参见commit 786ef50,commit f07eeed,Luis Marsano (``)(2018年5月12日)。
(Junio C Hamano -- gitster
--于2018年5月30日在commit 017b7c5合并)
git-credential-netrc
:接受gpg
选项无论gpg.program选项如何,
git-credential-netrc
都用'gpg
'硬编码解密。 这是像Debian这样的分布上的一个问题,它将现代GnuPG称为其他东西,比如'gpg2
'
使用Windows:
(Git在其发行版中有一个gpg.exe
,但使用完整的GPG安装包括一个gpg-agent.exe
,它会记住与你的GPG密钥相关的密码。)
gpg4Win Lite
,最小的gnupg命令行界面(使用most recent gpg4win-vanilla-2.X.Y-betaZZ.exe
),并使用GPG安装目录完成PATH:
set PATH=%PATH%:C:\path\to\gpg
copy C:\path\to\gpg\gpg2.exe C:\path\to\gpg\gpg.exe
(注意'copy
'命令:Git需要一个Bash脚本来执行命令'gpg
'。由于gpg4win-vanilla-2
附带gpg2.exe
,你需要复制它。)
gpgp --import aKey
# or
gpg --gen-key
(确保将密码加到该密钥上。)
%PATH%
中的目录中:
cd c:\a\fodler\in\your\path
curl -o c:\prgs\bin\git-credential-netrc https://raw.githubusercontent.com/git/git/master/contrib/credential/netrc/git-credential-netrc
(是的,这是一个Bash脚本,但它可以在Windows上运行,因为它将由Git调用。)
machine a_server.corp.com
login a_login
password a_password
protocol https
machine a_server2.corp.com
login a_login2
password a_password2
protocol https
(不要忘记'protocol
'部分:'http
'或'https
',具体取决于您将使用的URL。)
gpg -e -r a_recipient _netrc
(您现在可以删除_netrc
文件,仅保留_netrc.gpg
加密文件。)
git config --local credential.helper "netrc -f C:/path/to/_netrc.gpg -v"
(注意'/
':C:\path\to...
根本不起作用。)(你可以先用-v -d
看看发生了什么。)
从现在开始,任何使用需要身份验证的HTTP(S)URL的Git命令都将解密该_netrc.gpg
文件并使用与您联系的服务器关联的登录名/密码。第一次,GPG会要求您提供GPG密钥的密码,以解密该文件。其他时候,第一次GPG呼叫自动启动的gpg-agent将为您提供该密码。
这样,您可以在一个文件中记住多个URL /登录/密码,并将其存储在磁盘上加密。 我发现它比“缓存”助手更方便“,你需要记住并为每个远程服务键入(每个会话一次)一个不同的密码,以便将所述密码缓存在内存中。
有一种简单,老式的方法可以在HTTPS URL中存储用户凭据:
https://user:[email protected]/...
您可以使用git remote set-url <remote-repo> <URL>
更改网址
这种方法的明显缺点是您必须以纯文本格式存储密码。您仍然可以输入用户名(https://[email protected]/...
),这至少可以节省一半的麻烦。
您可能更喜欢切换到SSH或使用GitHub客户端软件。
使用凭证存储。
对于OS X和Linux上的Git 2.11+,use Git's built in credential store:
git config --global credential.helper libsecret
对于Windows上的msysgit 1.7.9+:
git config --global credential.helper wincred
对于OS X上的Git 1.7.9+使用:
git config --global credential.helper osxkeychain
你可以使用
git config credential.helper store
当您下次使用pull或push输入密码时,它将作为纯文本存储在文件.git-credentials中(有点不安全,但只需将其放入受保护的文件夹中)。
就是这样,如本页所述:
对我来说,我不需要首先下载帮助程序!我在Atlassian's Permanently authenticating with Git repositories找到了credential.helper下载。
引用:
如果要在OS X上使用带有凭据缓存的Git,请执行以下步骤:
下载二进制git-credential-osxkeychain。
运行以下命令以确保二进制文件是可执行的:
chmod a+x git-credential-osxkeychain
把它放在/usr/local/bin
目录中。
运行以下命令:
git config --global credential.helper osxkeychain
只需将登录凭据作为URL的一部分包含在内:
git remote rm origin
git remote add origin https://username:[email protected]/path/to/repo.git
注意:我不推荐这种方法,但是如果你匆忙而没有其他工作,你可以使用这种方法。
在GNU / Linux设置上,〜/ .netrc也能很好地工作:
$ cat ~/.netrc
machine github.com login lot105 password howsyafather
这可能取决于Git用于HTTPS传输的网络库。