yaoningvital / blog

my blog
31 stars 4 forks source link

解决git 以 https 方式每次都要输入用户名密码的问题 #202

Open yaoningvital opened 4 years ago

yaoningvital commented 4 years ago

一、问题场景

git credential-manager uninstall 清除了缓存在 git 中的用户名和密码,然后以后每次用 https 方式 clone 代码都需要输入 用户名和密码。

二、解决方法

怎样能解决这个问题呢?执行下面的命令:

git config --system --unset credential.helper
git config --global credential.helper store

三、相关命令

1、如何区分我们使用的是 https 方式还是 ssh 方式 跟 git server 交互的呢?

使用下面的命令:

git remote -v

比如得到如下结果:

think@think-PC MINGW64 /e/zny-projects/zny-boilerplate (master)
$ git remote -v
origin  https://hyperv28.msdi.cn/tfs/Power5DBIM/DigitalEngineeringPlatform/_git/zny-boilerplate (fetch)
origin  https://hyperv28.msdi.cn/tfs/Power5DBIM/DigitalEngineeringPlatform/_git/zny-boilerplate (push)

image

可以知道使用的是 https 协议,所以这个仓库是使用 https 方式跟 git server 进行交互的。

2、长期存储密码

git config --system --unset credential.helper
git config --global credential.helper store

上面的第一条命令的作用应该是复原 credential.helper 的设置。 第二条命令的作用是长期存储密码。

设置之后,在 C:\Users\登录用户名 目录下的 .gitconfig 文件中,记录了设置的这些信息,比如如下所示:

[user]
    name = yaoning
    email = ning.yao@ikang.com
[core]
    autocrlf = false
[credential]
    helper = store

image

3、设置缓存密码

git config --global credential.helper cache

设置记住密码,默认15分钟。

设置之后,C:\Users\登录用户名 目录下的 .gitconfig 文件中的内容变为如下所示:

[user]
    name = yaoning
    email = ning.yao@ikang.com
[core]
    autocrlf = false
[credential]
    helper = cache

4、设置缓存密码的时间

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

这样就设置了300s 之后缓存失效。

此时查看 C:\Users\登录用户名 目录下的 .gitconfig 文件中的内容如下:

[user]
    name = yaoning
    email = ning.yao@ikang.com
[core]
    autocrlf = false
[credential]
    helper = cache