首页 > 解决方案 > bash 脚本从 bash 脚本添加 git 凭据

问题描述

我需要能够将 git 凭据从我的 bash 脚本添加到我的 bash 脚本中,但不知道如何执行此操作。

git clone https://xxxxxxx

会询问我的用户名和密码。

我如何在 bash 脚本中传递这些?

任何指针将不胜感激

标签: linuxbashshellscripting

解决方案


对于基本 HTTP 身份验证,您可以:

  1. 在 url 中传递凭据:

    git clone http://USERNAME:PASSWORD@some_git_server.com/project.git
    

    警告这不安全:当您使用远程仓库时,您机器上的其他用户可以看到带有凭据的 URLps或实用程序。top

  2. 使用gitcredentials

    $ git config --global credential.helper store
    $ git clone http://some_git_server.com/project.git
    
    Username for 'http://some_git_server.com': <USERNAME>
    Password for 'https://USERNAME@some_git_server.com': <PASSWORD>
    
  3. 使用~/.netrc

    cat >>~/.netrc <<EOF
    machine some_git_server.com
           login <USERNAME>
           password <PASSWORD>
    EOF
    

推荐阅读