Git 推送或拉取时记住用户名密码
Git 推送或拉取时记住用户名密码
1、使用 HTTP(S) 协议远程库时
(1)使用 HTTPS 协议,有一种简单粗暴的方式是在远程地址中带上密码。
git remote set-url origin http://yourname:password@bitbucket.org/yourname/project.git(2)还有一种方法,是创建文件存储 Git 用户名和密码。
以 Windows 环境为例,在 %USER_HOME% 目录中(一般为 C:\Users\yourname),打开 Git Bash 命令行,创建文件并修改:
touch .git-credentials
vim .git-credentials在文件中输入仓库域名,这里使用了 bitbucket.org:
https://yourname:password@bitbucket.org在 CMD 终端中设置全局 Git 环境,长期存储密码(该命令会同时生成一个 .gitconfig 文件):
git config --global credential.helper store执行完之后,.gitconfig 文件中会多了一项:
[credential]
helper = store其他设置密码方式:
记住密码(默认 15 分钟):git config --global credential.helper cache
自定义存储时间:git config credential.helper 'cache --timeout=3600'
2、使用 SSH 协议远程库时
如果原来的推送地址协议是 HTTPS,可以通过换成 SSH 协议,在远程仓库添加 SSH Key 来实现推送时免账户密码输入。
git remote -v // 查看远程地址
git remote rm origin // 删除原有的推送地址
git remote add origin git@github.com:<用户名>/版本库名或者
git remote -v
git remote set-url origin git@github.com:<用户名>/版本库名执行推送
git push -u origin master发现提示权限不够
The authenticity of host 'bitbucket.org (104.192.143.1)' can't be established.
RSA key fingerprint is SHA256:zzXQOXSRBEiUtuE8AikJYKwbHaxvSc0ojez9YXaGp1A.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'bitbucket.org,104.192.143.1' (RSA) to the list of kn
own hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.需要在本地创建该帐号的 RSA Key。可以参考以下两篇文章:
然后再执行推送就可以了。
3、配置 SSH 连接 Git(延伸篇)
3.1、概念
(1)HTTP 与 SSH 的区别
HTTP:对于初学者来说使用比较方便,通过密码方式来验证,clone时每次都需要输入密码,也可通过上面的方式去记住密码。SSH:在clone之前需要提前配置好SSH Key,clone时需要输入密语(不同于密码)来验证,之后就不再需要身份验证。
(2)id_rsa 和 id_rsa.pub
id_rsa为私钥,id_rsa.pub为公钥。我们会把公钥存到服务端,私钥存在本地,当需要从服务端请求内容的时候,服务器会验证我们本地的私钥是否匹配。
(3)known_hosts 文件
当我们成功与服务端进行连接时,
ssh会记录服务端的Host、IP以及rsa文件,当连接过程中出现:Permanently added '10.0.0.0' (RSA) to the list of known hosts.
如果你选择Yes,那这个known_hosts文件中就会多出一条记录。Windows是不会自动产生这个文件的,也可能是程序产生了,但是没权限写入,所以我们没有在.ssh/目录下看到这个文件。但如果我们创建了这个文件,会发现里面的内容会随着我们的验证慢慢增加。
3.2、创建 SSH Key
打开 Git Bash 命令窗口,输入:
ssh-keygen -t rsa -C "your_email@example.com"参数:
-t用来指定密钥类型,默认是rsa,可以省略。-C设置注释文字,比如邮箱。-f指定密钥文件存储文件名(慎用,容易出现异常)。
以上代码省略了 -f 参数,因此,运行上面那条命令后会让你输入一个文件名,用于保存刚才生成的 SSH Key:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/you/.ssh/id_rsa):(回车)当然,你也可以输入文件名(输入之后会默认在当前目录下创建秘钥文件),不过还是推荐默认的名称;接着又会提示让你输入设置两次密语字符串(不是 Git 用户的密码),设置完成后即提示:
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/you/.ssh/id_rsa.
Your public key has been saved in /c/Users/you/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:chygCivWHDys+Y2m4N6mX2+RsPjC47E9uW5RI9KqPO4 your_email@example.com
The key's randomart image is:
+---[RSA 2048]----+
| . |
| o . . |
|. =o . |
| o=oo+ + . |
|o+.o+ * S |
|o .ooo = |
|o o=.oo . |
|o++===.. |
|+E*==++. |
+----[SHA256]-----+看到以上信息说明 SSH Key 已经创建成功,并在用户目录下创建了 id_rsa 和 id_rsa.pub 文件。
3.3、把 Key 添加到 GitHub 上
(1)打开 id_rsa.pub 文件,复制所有内容。
(2)网页登录你的 Git,右上角 Settings --> SSH Keys --> 粘贴到对应的输入框中,然后保存即可(title 为备注,可以自动识别或者手动随便输入)。
3.4、测试 SSH
ssh -T git@gitlab.com注意:如果此时提示你输入的是密码
password,而不是密语passphrase,那就不要想了,程序没有找到你的私钥!

