文档首页> 代码托管 CodeArts Repo> 用户指南> 更多Git知识> 使用HTTPS协议设置免密码提交代码
更新时间:2023-05-05 GMT+08:00
分享

使用HTTPS协议设置免密码提交代码

背景信息

为避免每次访问都输入用户名和密码,可以使用Git的凭证存储功能实现免密码访问(为保证该功能正常使用,建议安装Git2.5以上版本),不同操作系统的设置方法如下:

前提条件

  • 使用HTTPS协议方式在进行Git clone、Git fetch、Git pull以及Git push等操作时,需要输入代码托管的用户名和密码。

Windows系统如何使用HTTPS协议设置免密码提交代码

Windows系统设置免密码提交代码方法,如下表所示。

表1 Windows系统设置免密码提交代码

方法

操作说明

本地配置HTTPS密码

  1. 设置Git验证方式。

    打开Git客户端:git config --global credential.helper store

  2. 使用Git命令进行首次Clone或Push代码,根据提示填写用户名和密码。
  3. 打开“.git-credentials”文件,如果已在本地存储了用户名和密码,会出现如下记录:
    https://username:password@***.***.***.com

MAC系统如何使用HTTPS协议设置免密码提交代码

安装“osxkeychain”工具来实现免密码访问:

  1. 查找当前系统是否已经安装该工具。
    git credential -osxkeychain
    # Test for the cred helper
    Usage: git credential -osxkeychain < get|store|erase >

    如果回显如下,则为未安装。

    git: 'credential -osxkeychain' is not a git command. See 'git --help'. 
  2. 如果该工具未安装,先获取安装包:
    git credential -osxkeychain
    # Test for the cred helper
    git: 'credential -osxkeychain' is not a git command. See 'git --help'.
    curl -s -o \
    https://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain
    # Download the helper
    chmod u+x git-credential-osxkeychain
    # Fix the permissions on the file so it can be run
  3. “osxkeychain”安装在Git的同一个目录下:
    sudo mv git-credential-osxkeychain\
    "$(dirname $(which git))/git-credential-osxkeychain"
    # Move the helper to the path where git is installed
    Password:[enter your password]
  4. 使用“osxkeychain”工具将Git配置成免密码模式:
    git config --global credential.helper osxkeychain
    #Set git to use the osxkeychain credential helper

    第一次执行操作时会提示输入密码,输入后将由“osxkeychain”管理用户名和密码,后续再执行Git操作时将不再需要密码校验。

Linux系统如何使用HTTPS协议设置免密码提交代码

Linux系统提供两种可选的免密码访问模式:

  • cache模式:
    • 将凭证存放在内存中一段时间,密码永远不会被存储在磁盘中,并且在15分钟后从内存中清除:
      git config --global credential.helper cache
      #Set git to use the credential memory cache
    • 通过timeout选项设置过期时间,单位为秒:
      git config --global credential.helper 'cache --timeout=3600'
      # Set the cache to timeout after 1 hour (setting is in seconds)
  • store模式:

    将凭证用明文的形式存放在磁盘“home”目录下(默认是“~/.git-credentials”),永不过期,除非手动修改在Git服务器上的密码,否则永远不需要再次输入凭证信息。“git-credentials”文件内容如下:

    https://username:password@***********.com

    保存退出后,执行如下命令即可完成:

    git config --global credential.helper store

报错处理

如果在使用HTTPS协议下载时提示“SSL certificate problem: self signed certificate”错误信息,请在客户端进行如下设置:

git config --global http.sslVerify false
分享:

    相关文档

    相关产品