简体   繁体   English

bash脚本从bash脚本添加git凭据

[英]bash script adding git credentials from bash script

I need to be able to add git credentials from my to my bash script but can't figure out how to do this. 我需要能够从我的bash脚本添加git凭据,但无法弄清楚如何执行此操作。

git clone https://xxxxxxx

would ask for my username name and password. 会要求我的用户名和密码。

how to i pass these in a bash script ? 如何在bash脚本中传递这些?

any pointers would be appreciated 任何指针将不胜感激

For basic HTTP authentication you can: 对于基本HTTP身份验证,您可以:

  1. Pass credentials inside url: 在url中传递凭据:

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

    WARN this is not secure: url with credentials can be seen by another user on your machine with ps or top utilities when you work with remote repo. 警告这不安全:当您使用远程仓库时,使用pstop实用程序的机器上的其他用户可以看到带凭据的URL。

  2. Use gitcredentials : 使用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. Use ~/.netrc : 使用~/.netrc

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

1) This may help you add credentials git 1)这可以帮助您添加凭据git

2) I currently work with gitlab and I have it in a container with jenkins, anyway to do the clone I do this: http://<user_gitlab>@ip_gitlab_server/example.git 2)我目前正在使用gitlab并将其放在带有jenkins的容器中,无论如何要做克隆我这样做: http://<user_gitlab>@ip_gitlab_server/example.git

I hope I help you 我希望我帮助你

You can still pass in the username and password into the URL for git clone : 您仍然可以将用户名和密码传递到git clone的URL:

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

As for using a bash script, You can pass the username $1 and password $2 : 至于使用bash脚本,您可以传递用户名$1和密码$2

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

Then call the script with: 然后调用脚本:

./script.sh username password

Addtionally, It might be more secure to leave the password out and only include the username: 另外,将密码保留在外并且只包含用户名可能更安全:

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

Since the command with your password will be logged in your bash history. 由于带有您密码的命令将记录在您的bash历史记录中。 However, you can avoid this by adding a space in front of the command. 但是,您可以通过在命令前添加空格来避免这种情况。

You can also use How do I parse command line arguments in Bash? 您还可以使用如何在Bash中解析命令行参数? for nicer ways to use command line arguments. 有更好的方法来使用命令行参数。

Also be careful to use URL Encoding for special characters in usernames and passwords. 另外,请注意对用户名和密码中的特殊字符使用URL编码 A good example of this is using %20 instead of @ , since URLS need to use standard ASCII encoding for characters outside the standard character set. 一个很好的例子是使用%20而不是@ ,因为URLS需要对标准字符集之外的字符使用标准ASCII编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM