[英]bash script adding git credentials from bash script
我需要能够从我的bash脚本添加git凭据,但无法弄清楚如何执行此操作。
git clone https://xxxxxxx
会要求我的用户名和密码。
如何在bash脚本中传递这些?
任何指针将不胜感激
对于基本HTTP身份验证,您可以:
在url中传递凭据:
git clone http://USERNAME:PASSWORD@some_git_server.com/project.git
警告这不安全:当您使用远程仓库时,使用ps
或top
实用程序的机器上的其他用户可以看到带凭据的URL。
使用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
1)这可以帮助您添加凭据git
2)我目前正在使用gitlab并将其放在带有jenkins的容器中,无论如何要做克隆我这样做: http://<user_gitlab>@ip_gitlab_server/example.git
我希望我帮助你
您仍然可以将用户名和密码传递到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
而不是@
,因为URLS需要对标准字符集之外的字符使用标准ASCII编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.