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

YangeIT大约 2 分钟

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

一、背景:

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

二、解决方法:

1、ssh方式:

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

2、免密拉取配置

(1)cd到~/目录下,创建一个文件:touch .git-credentials vim .git-credentials (2)然后输入https://{username}:{password}@git.gitxx.comopen in new window,比如http://fengjiaheng:password@git.gitxx.comopen in new window (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@链接open in new window 修改为 git clone https://username:password@链接open in new window

示例: git clone git@http://112.12.122.22open in new window:t-mapi/hotel-tapi.git 修改为 git clone ‘http://username:password@112.12.122.22open in new window:t-mapi/hotel-tapi.git’

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

三、总结:

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

版权声明:本文为CSDN博主「业余敲代码」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_39676449/article/details/126122939open in new window