对于SSH方式,免密码登录很简单,但是对于http协议则有些困难,它的每一个连接都需要用户名和密码,而这在双重认证时更为麻烦,因为需要输入一个随机生成的,且毫无规律的token作为密码。
Git拥有一个凭证系统可以处理这类事情,但是:
默认,所有都不缓存,因此每一次连接都会询问用户名和密码
“cache”模式:会将凭证放在内存一段时间,并且15分钟后从内存清除
“store”模式:以明文形式存放在硬盘,且永不过期。
对于Mac,Git有“osxkeychain”模式,它将凭证缓存到系统用户的钥匙串。凭证存放在磁盘中,永不过期,且被加密,这与存放HTTPS凭证及Safari自动填写是相同的。
在Windows中,可以安装winstore辅助工具,它与"osxkeychain"类似,但它是使用Windows Credential Store控制敏感信息。
$ git config --global credential.helper cache
store模式
store模式接受--file <path> 参数,可以自定义存放密码的路径,默认是~/.git-credentials
$ git config --global credential.helper store --file ~/.my-credentials
cache模式
cache模式有--timeout <seconds> 参数,可以设置存活时间,默认是900,也就是15分钟。
Git允许配置多种凭证方式,可以在.gitconfig 配置
[credential]
helper = store --file /mnt/thumbdrive/.git-credentials
helper = cache --timeout 30000
git credential
git credential fill 命令会话
$ git credential fill (1)
protocol=https (2)
host=mygithost
(3)
protocol=https (4)
host=mygithost
username=bob
password=s3cre7
$ git credential fill (5)
protocol=https
host=unknownhost
Username for 'https://unknownhost': bob
Password for 'https://bob@unknownhost':
protocol=https
host=unknownhost
username=bob
password=s3cre7
空行代表输入已经完成,凭证系统会输出它所知道的信息。
凭证系统实际调用的程序与Git本身相关,具体与credential.helper 配置的值有关
| 配置值 | 行为 |
|---|---|
| foo | 执行git-credential-foo |
| foo -a --opt=bcd | 执行git-credential-foo -a --opt=bcd |
| /absolute/path/foo -xyz | 执行/absolute/path/foo -xyz |
| !f() { echo "password=s3cre7"; };f | !后面的代码会在shell执行 |