如何在脚本中提供用户名/密码git克隆,但不在.git / config中存储凭据

我在这样的脚本克隆git仓库:

git clone https://user:[email protected]/name/.git 

这工作,但我的用户名和密码! 现在存储在.git/config中的原始URL中。

我怎样才能防止这一点,但仍然在一个脚本(所以没有手动input密码)这样做?

    我使用的方法是实际使用git pull而不是克隆。 该脚本将如下所示:

     mkdir repo cd repo git init git config user.email "email" git config user.name "user" git pull https://user:[email protected]/name/repo.git master 

    这不会将您的用户名或密码存储在.git/config

    我会做的一个进一步的build议(如果你不能使用SSH)是实际使用OAuth令牌,而不是明文用户名/密码,因为它更安全一些。 您可以通过个人资料设置生成OAuth令牌: https : //github.com/settings/tokens 。

    然后使用该标记拉命令将是

     git pull https://$OAUTH_TOKEN:[email protected]/name/repo.git master 

    你可以在你的~/.netrc文件中input你的连接信誉。 有些东西是:

     machine host.example.net login bart password eatmyshorts 

    只要确保chmod该文件为600.如果您使用Windows,以下链接可能是有用的: https : //stackoverflow.com/questions/6031214/git-how-to-use-netrc-file-on- Windows到保存用户和密码

    就个人而言,我倾向于使用SSH密钥来进行authentication(当然,如果您被允许的话)。