Git 之 配置文件与用户凭证

配置文件

Git的配置文件有三个:

  • 系统配置: /private/etc/gitconfig
  • 用户配置: ~/.gitconfig
  • 项目配置:.git/config

用户凭证

由于Git和Github交互操作可能会很频繁,那么一定少了用户授权的操作,为了防止每次操作重复输入用户名和密码,Git提供了两种解决方法:

    • 秘钥
      首先创建一对秘钥  ssh-keygen -t rsa,然后将 id_rsa.pub (公钥)内容拷贝到github中,日后操作无需再输入用户名和密码。
      注意:这种方式需要使用GIt中 [email protected]:yaya/xxxxx.git 格式地址。
    • 密码
      Https访问git时,避免每次操作需要输入用户名和密码,可以在配置文件中添加如下配置项:
          [credential]
          helper = store/cache/第三方

      store:
              表示将用户名和密码保存在硬盘上
              第一次输入过用户名和密码之后,用户名和密码就会保存在当前用户根目录的 .git-credentials 文件中,内容格式为:https://用户名:密码@github.com

              自动添加配置命令:git config credential.helper store
      cache: 
              表示将用户名和密码保存在缓存中
              第一次输入过用户名和密码之后,用户名和密码就会保存在缓存中,默认超时时间是 900 秒,缓存相关文件保存在当前用户根目录的 git-credential-cache 中 
              自动添加配置命令:
                     git config credential.helper cache
                     git config credential.helper 'cache --timeout=300'

      相关操作:
              清除缓存:git credential-cache exit 
              指定超时:
                  [credential]
                  helper = cache --timeout=300
      注意:
               这种方式需要使用GIt中 https://github.com/WuPeiqi/xxxx.git 格式地址。
               指定用户名和密码: https://用户名:密码@github.com/yaya/xxx.git 

猜你喜欢

转载自www.cnblogs.com/gaoya666/p/9146122.html