git凭证存储

2017-09-06

23333

git使用http协议传输时每次需要输入用户名密码,我又比较懒,刚好zzz同学发了个解决方法,抽空深入了解了一下。

TripleZ's blog

解决方法

git 配置 (gitcredentials)

  • gitcredentials - providing usernames and passwords to Git

    DOC

  • git 凭证存储策略

    • 默认不存储

    • "cache"模式

      • 命令git config --global credential.helper cache默认存储15分钟

      • 自定义存储时间git config credential.helper 'cache --timeout=3600'

    • "store"模式

      • 凭证长期但以明文存储在home目录
    • Mac git的"osxkeychain"模式
      • 凭证长期且解密存储在磁盘上
    • windows "winstore"工具
      • 与Mac上类似,长期且加密存储
  • 用法
    • git 的配置选项
      1. git config --global credential.helper [cache --timeout=3600|store --file=/.../example/filename|osxkeychain]
        括号中其中一个模式
      2. .gitconfig文件
        [credential]
        helper = store --file /.../example/filename
        helper = cache --timeout 36000
        ...

        git 按配置文件中的顺序搜索直至找到可用凭证
  • 示例
    $ git config credential.helper store
    $ git push http://example.com/repo.git
    Username: 
    Password: 
    
    [several days later]
    $ git push http://example.com/repo.git
    [your credentials are used automatically]
    

git credential

  • git-credential - Retrieve and store user credentials

    git config --global credential.helper 配置credential时修改了git credential 的配置文件

    git credentials文档

  • 与 git 相互独立的 git credential 工具

    git credential [fill|approve|reject]
    
  • I/O format
    • protocol (e.g.,https)

    • host remote hostname

    • path repository's path on the server

    • username credential's username (e.g.,github username)

    • password credential's password (e.g.,github password)

    • url could included almost all attribute.

      • eg:url=https://username@example.com will be parsed as protocol=https , username=username , host=example.com

      • WARN:if define "url" at last,the above attributes could be rewrite

        protocol=http
        host=aaa.com
        username=foxwest
        url=https://403forbidden.website
        

        the config would be parsed as

        protocol=https
        username=NULL(empty)
        host=403forbidden.website
        

git credential-manager (GCM on windows)

关于自定义凭证存储命令