git clone 主要两种,一种是基于ssh,另一种基于https。
基于 ssh
对于ssh的,在云上的环境就不是很适合,因为实例总是不断申请释放,每次都需要重新生成id_rsa.pub 文件,然后在对应的 git网站上将这个文件内容拷贝到对应git服务器网站的 ssh keys
中,想要自动化似乎有点麻烦。
但是这里还是先说以下操作步骤:
- 在host主机上输入以下命令
ssh-keygen
,然后不断回车,会在~/.ssh
文件夹下生成id_rsa.pub
文件 - 在git网站上(如github)的设置里,将
id_rsa.pub
文件添加到ssh keys
中即可 - 然后 git clone时选择 ssh协议的链接。
基于https
这个方法有利于实现自动化,就是直接将账户名和密码明文写在文件中即可。当然安全性差点,不过一般来说够用吧。
接下来说一下操作步骤
echo "https://{username}:{password}@github.com" > ~/.git-credentials
git config --global credential.helper store
git clone https://www.github.com/username/repository
其中第二步,也可以直接通过修改文文件 .gitconfig 进行。直接修改.gitconfig 文件,一次性把所有配置写入该文件也挺方便
cat > ~/.gitconfig << EOF
[credential]
helper = store
[user]
name = username
email = youremail
EOF
基于access token
上面直接把密码明文写入的方式不安全,github 在2021 年8月13后就把这种方式给禁了。补充一下怎么基于personal access token。这个token怎么生成可以百度一下,比较简单,在setting->developer settings下面。
然后与操作与上面其实是一样的,只不过把密码换成token就行了。
- echo “https://{username}:{token}@github.com” > ~/.git-credentials
- 修改gitconfig 文件
cat > ~/.gitconfig << EOF
[credential]
helper = store
[user]
name = username
email = youremail
EOF
- git clone
以上两种方式都能实现免密钥登录,在云环境下,第二种更有利于实现自动化。