MENU

Git Pull 避免用户名和密码方法

December 22, 2016 • GIT

记录不使用ssh-key方式pull或者push代码的时候免密码的问题。

第一种方式

git config --global credential.helper store

执行完成后会在家目录下多出一个.gitconfig的文件,内容为:

[credential]
    helper = store //store表示永久存储

此时切换到你的项目根目录执行一次git pull那么这个时候会提示你输入用户名和密码,git会把这次的用户名和密码记录到当前登录用户家目录的.git-credentials文件中,如下:

https://username:password@git.coding.net

那么你下一次再次执行git pull的时候就不要求你输入密码了。

通过这种方式配置的http或者https免密码方式还可以设置密码有效期,比如:

git config --global credential.helper cache

上面设置默认密码存储时间为15分钟,如果你想自己指定时间可以是使用--timeout选项:

git config credential.helper 'cache --timeout=3600'

第二种方式(推荐)

增加远程地址的时候带上用户名和密码
首先切换到你的项目根目录,执行:

sudo git remote remove origin
sudo git remote add origin https://username:password@git.coding.net/lepig/laravel.git

至此就免密码进行git pull了。

Last Modified: November 10, 2019
Leave a Comment