转载请标明出处:http://blog.csdn.net/xx326664162/article/details/49686241 文章出自:薛瑄的博客
你也可以查看我的其他同类文章,也会让你有一定的收货!
前言
使用Git 向远程仓库(例如:GitHub,gitee)提交代码 ,需要输入账号和密码。可能会遇到这样的情况密码输错一次,想修改,但是不知道去哪里修改。一直报错fatal: Authentication failed for
又不弹出用户名和密码输入框 。
你需要了解Git是如何保存账号密码的,也就是凭据管理。下面将图文一一介绍
Git凭据管理的三种方式
Git的凭据存储有cache、store、manager三种方式
Git 中有三种级别system 、global 、local ,可以针对不同的级别设置不同的凭据存储方式
查看不同级别的凭据管理方式
查看不同级别的credential.helper
<span style="color:#000000"><code class="language-git">Jant@Jant MINGW64 ~/Desktop
$ git config --system credential.helper
manager
Jant@Jant MINGW64 ~/Desktop
$ git config --global credential.helper
cache
Jant@Jant MINGW64 ~/Desktop
$ git config --local credential.helper
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
global 、local 如果不设置,默认是没有的
修改指定级别的凭据管理方式
<span style="color:#000000"><code class="language-git">Jant@Jant MINGW64 ~/Desktop
$ git config --system credential.helper wincred
Jant@Jant MINGW64 ~/Desktop
$ git config --system credential.helper
wincred
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- git-credential-manager.exe 下面简称manager
- git-credential-store.exe 下面简称stroe
- git-credential-wincred.exe 下面简称wincred
相信你现在有几个疑问,我平时输入账号密码,用的是哪种?账号密码保存在哪里?如何进行修改?
下面我们就来逐一分析
一、manager
若安装Git时安装了GitGUI,自动会在system级别中设置credential.helper为manager。
git-credential-manager.exe和 git-credential-wincred.exe 都是将凭据存储在系统的凭据管理器中,只不过前者是有图形界面,后者没有图形界面,需要在命令行中输入密码
使用场景
你的账号密码是在弹窗中输入的,例如下面的这样。使用的就是manager来进行凭据管理的
凭据保存的位置
在控制面板->用户账户->凭据管理器,可以看到对应的git账号凭据管理,可以修改或者删除。
删除后,再次pull或者push,会重新输入密码
二、stroe
如果你在输入账号密码的时候,关闭了manager 方式的输入框,就会要求你在命令行中输入账号,如下图
使用场景
账号输入:
密码输入:
存储密码
1、 设置global 的凭据管理方法为store ,命令如下:
<span style="color:#000000"><code>git config --global credential.helper store
</code></span>
- 1
在C:/user/用户名
目录下的.gitconfig文件添加以下内容
<span style="color:#000000"><code>[credential]
helper = store
</code></span>
- 1
- 2
- 3
2、输入账号和密码,push或者pull后,此时查看C:/user/用户名 会自动生成.git-credentials 文件,里面明文保存了你的账号和密码。
注意:系统自动的.git-credentials文件和自己手动写入.git-credentials 文件,符号编码是不一样导致,所以自己手动生成该文件,可能是无法使用的
特别注意:如果使用store存储凭据,会在.git-credentials 文件和控制面板的凭据管理器中,两个地方同时生成凭据。所以如果要修改,最后两处一起删除
三、cache
将凭证存放在内存中一段时间。 密码永远不会被存储在磁盘中,并且默认在15分钟后从内存中清除。
<span style="color:#000000"><code>git config --global credential.helper cache
</code></span>
- 1
如果想自己设置时间,可以这样做:
<span style="color:#000000"><code>git config credential.helper 'cache --timeout=3600'
</code></span>
- 1
密码将保存在内存中1小时
参考:
https://www.jianshu.com/p/0ad3d88c51f4
https://www.cnblogs.com/volnet/p/git-credentials.html
http://git.oschina.net/oschina/git-osc/issues/2586
http://yongqing.is-programmer.com/posts/80371.html