簡體   English   中英

bash腳本從bash腳本添加git憑據

[英]bash script adding git credentials from bash script

我需要能夠從我的bash腳本添加git憑據,但無法弄清楚如何執行此操作。

git clone https://xxxxxxx

會要求我的用戶名和密碼。

如何在bash腳本中傳遞這些?

任何指針將不勝感激

對於基本HTTP身份驗證,您可以:

  1. 在url中傳遞憑據:

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

    警告這不安全:當您使用遠程倉庫時,使用pstop實用程序的機器上的其他用戶可以看到帶憑據的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 

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM