我需要在我的bash脚本中添加git凭据,但是不知道如何实现。
git clone https://xxxxxxx
会要求输入我的用户名和密码。
我该如何在Bash脚本中传递它们?
欢迎提供任何指导。
对于基本的HTTP身份验证,您可以:
在URL中传递凭据:
git clone http://USERNAME:PASSWORD@some_git_server.com/project.git
警告:这不是安全的操作,带有凭证的 URL 可能会被其他用户通过 ps
或 top
实用程序在您使用远程资源库时看到。
请使用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>
使用~/.netrc
:
cat >>~/.netrc <<EOF
machine some_git_server.com
login <USERNAME>
password <PASSWORD>
EOF
您仍然可以在 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历史记录中。但是,您可以在命令前面添加一个空格来避免这种情况。
您还可以使用如何解析Bash中的命令行参数?以更好的方式使用命令行参数。
此外,请注意在用户名和密码中使用URL编码来处理特殊字符。一个很好的例子是使用%20
代替@
,因为URL需要使用标准ASCII编码来处理标准字符集之外的字符。
1) 这可能会帮助您添加git凭据
2) 我目前使用gitlab,并将其与jenkins一起放在容器中,无论如何进行克隆,我都会这样做:http://<user_gitlab>@ip_gitlab_server/example.git
希望能帮到您。