【问题标题】:bash script adding git credentials from bash scriptbash 脚本从 bash 脚本添加 git 凭据
【发布时间】:2019-05-23 15:57:17
【问题描述】:

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

git clone https://xxxxxxx

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

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

任何指针将不胜感激

【问题讨论】:

标签: linux bash shell scripting


【解决方案1】:

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

  1. 在 url 中传递凭据:

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

    警告这不安全:当您使用远程存储库时,您计算机上的其他用户可以使用 ps 或 top 实用程序看到带有凭据的 URL。

  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
    

【讨论】:

    【解决方案2】:

    您仍然可以将用户名和密码传递到git clone 的 URL:

    git clone https://username:password@github.com/username/repository.git
    

    至于使用 bash 脚本,你可以传递用户名$1 和密码$2:

    git clone https://$1:$2@github.com/username/repository.git
    

    然后调用脚本:

    ./script.sh username password
    

    另外,不输入密码而只包含用户名可能更安全:

    git clone https://$1@github.com/username/repository.git
    

    因为带有您密码的命令将记录在您的 bash 历史记录中。但是,您可以通过在命令前面添加一个空格来避免这种情况。

    您还可以使用How do I parse command line arguments in Bash? 来更好地使用命令行参数。

    还要小心在用户名和密码中使用URL Encoding 作为特殊字符。一个很好的例子是使用%20 而不是@,因为URLS 需要对标准字符集之外的字符使用标准ASCII 编码。

    【讨论】:

      【解决方案3】:

      1) 这可能会对您有所帮助add credentials git

      2) 我目前使用 gitlab,我将它放在一个带有 jenkins 的容器中,无论如何要克隆我这样做:http://&lt;user_gitlab&gt;@ip_gitlab_server/example.git

      希望能帮到你

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-20
        • 2012-06-07
        • 2012-01-18
        • 1970-01-01
        • 1970-01-01
        • 2012-09-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多