Git 中 ssh key、https 配置及用户名、邮箱基本信息配置
source link: https://lianpf.github.io/posts/%E5%BC%80%E5%8F%91%E6%97%A5%E8%AE%B0/06.git%E4%B8%ADssh%E4%B8%8Ehttps/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
git 作为当前最受欢迎的版本控制软件,使用是很频繁的。但是…
git 作为当前最受欢迎的版本控制软件,使用是很频繁的。但是有的小伙伴发现每次使用 git push 等操作时都要输入密码,实在是麻烦。这里对使用 SSH 与 HTTPS 两种通讯协议介绍一下免密码配置
一、本地Git
基本信息配置
本地
Git
用户名、邮箱等基本信息配置
(1) 查看
验证『当前本地』是否已配置账号信息
$ git config --list // 查看所有
$ git config --global --list // 查看当前用户(global)配置
$ git config --system --list // 查看系统config
$ git config --local --list // 查看当前仓库配置信息
// 查看指定信息
$ git config user.name
$ git config user.email
(2) 新增
第一次配置,需要设置Git
的user name
和email
$ git config --global user.name "lian" // you name
$ git config --global user.email "[email protected]" //you email
(3) 修改
// 覆盖
$ git config --global user.name "yourName"
$ git config --global user.email "[email protected]"
// 替换
$ git config --global --replace-all user.name "yourName"
$ git config --global --replace-all user.email "[email protected]"
(4) 删除
$ git config --global --unset user.name "yourName"
$ git config --global --unset user.email "[email protected]"
二、ssh key
和https
差异
clone
项目: 使用ssh
方式时,首先你必须是该项目的管理者或拥有者,并且需要配置个人的ssh key
。而对于使用https
方式来讲,就没有这些要求。push
:在使用ssh
方式时,是不需要验证用户名和密码,如果你在配置ssh key
时设置了密码,则需要验证密码。而对于使用https
方式来讲,每次push
都需要验证用户名和密码。
三、配置ssh key
1. 单个ssh key
生成及配置
1.1 生成密钥
ssh-keygen
程序在Linux/Mac
系统上由SSH
包提供,而在Windows
上则包含在MSysGit
包里
$ ssh-keygen -t rsa -C "[email protected]"
直接Enter
,会提示你输入密码,如下(可不输,建议输入,安全性更高):
Enter same passphrase again: [Type passphrase again]
『本地的密钥对』生成完成提示:
Your public key has been saved in /home/you/.ssh/id_rsa.pub.
The key fingerprint is: # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db [email protected]
得到两个文件:id_rsa
和id_rsa.pub
。不是第一次,则选择overwrite
1.2 检查是否存在ssh key
$ cd ~/.ssh
$ cd ~/.ssh && ls // 进入且查看
id_rsa
:密钥id_rsa.pub
:有.pub
后缀的文件就是公钥,另一个文件则是。
1.3 添加公钥到远程仓库(github、gitLab 等)
$ cat ~/.ssh/id_rsa.pub
ssh-rsa
AAAAB3NzaC1yc2EAAAAAQC0X6L1zLL4VHuvGb8aJH3ippTozmReSUzgntvk434aJ
/v7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8vR3c8E7CjZN733f5AL8uEYJA+YZevY5U
CvEg+umT7PHghKYaJwaCxV7sjYP7Z6V79OMCEAGDNXC26IBMdMgOluQjp6o6j2KAdtRBdCDS/QIU5THQD
xJ9lBXjk1fiq9tITo/aXBvjZeD+gH/Apkh/0GbO8VQLiYYmNfqqAHHeXdltORn8N7C9lOa/UW3KM7QdXo
6J0GFlBVQeTE/IGqhMS5PMln3 [email protected]
添加公钥到远程仓库
以gitHub
为例,登录进入个人中心,打开Settings -> SSH Keys
,复制公钥内容到『key
文本域』内
如果公钥复制正确, 『
title
域』会自动将you email
代入, 便于标识,你可以修改为其他的名字
用ssh命令
分别测试
ssh -T [email protected]
2. 多个SSH-Key
配置
当有多个『
git
账号』时,比如:一个gitLab
,用于公司内的工作开发。一个github
,用于个人的一些开发方向。
2.1 生成ssh key
// 生成一个gitlab用的SSH-Key
$ ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/id_rsa_gitlab
// 生成一个github用的SSH-Key
$ ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/id_rsa_github
2.2 生成config
配置文件
~/.ssh
目录下新建一个config
文件
$ cd ~/.ssh
// 为.ssh目录设置权限
.ssh $ chmod 600 ~/.ssh/config
config
文件,添加如下内容:
Host
和HostName
填写git
服务器的域名IdentityFile
指定私钥的路径
# gitLab
Host gitLab.com
HostName gitLab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
2.3 用ssh
命令分别测试
$ ssh -T [email protected]
$ ssh -T [email protected]
以gitHub
为例,成功返回如下内容:
$ ssh -T [email protected]
Hi lianpf! You've successfully authenticated, but GitHub does not provide shell access.
四、HTTPS 通信协议
待更新…g
最后, 希望大家早日实现:成为前端高手的伟大梦想!
欢迎交流~
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK