linux下解决 git clone每次都要输入用户名密码问题(推荐)

一、背景:

git clone代码或者push代码时候需要输入账号密码

二、解决方法:

1、ssh方式:

先用git config --global user.name 'username’和git config --global user.email '[email protected]’配置一下用户名和邮箱
生成ssh公钥:ssh-keygen -t rsa -C “[email protected]”,查看~/.ssh/id_rsa.pub文件内容,获取到你的public key,粘贴到GitLabssh公钥管理处即可
使用git clone http://git.gitxxx.com/xxx.git,先测试一下,看能不能拉取成功。如果成功,向下进行。此时还是会询问用户名和密码的。

2、免密拉取配置

(1)cd到~/目录下,创建一个文件:touch .git-credentials
vim .git-credentials
(2)然后输入https://{username}:{password}@git.gitxx.com,比如http://fengjiaheng:[email protected]
(3)然后执行:git config --global credential.helper store
(4)然后使用git config --list或者查看一下~/.gitconfig文件,会发现多了一行[credential] helper = store
(5)这时候再用 git 拉取仓库就不需要输入用户名和密码了。
注意:第4步必须要做,否则做完4、5步之后也不能免密码拉取成功,需要再次执行第4步骤。

3、粗暴使用型

Git Clone命令直接使用用户名密码Clone

git clone http://userName:password@链接
修改为 git clone https://username:password@链接

示例:
git clone git@http://112.12.122.22:t-mapi/hotel-tapi.git
修改为
git clone ‘http://username:[email protected]:t-mapi/hotel-tapi.git’

注意:

(1)http -> https
(2)如果账号username或者password中有@符号,需要 将@替换为%40
(3)如果报错git clone event not found,需要将 git clone 后地址加上引号 ‘’

三、总结:

1、第二种方法比较简单(推荐使用),但是第一二种方法中都首次都必须自己输入账号密码
2、第三种一次都不用输入账号密码(推荐使用)

猜你喜欢

转载自blog.csdn.net/jh035512/article/details/127980837