git生成ssh key和多账号支持
git配置ssh
1.首先设置git的全局user name和email
$ git config --global user.name "ygtzz"
$ git config --global user.email "ygtzz@123.com"
2.进入.ssh目录(没有就新建一个)
Windows cd C:/Users/userName/.ssh
Mac cd ~/.ssh
3.终端中执行ssh-keygen生成key
$ ssh-keygen -t rsa -C “ygtzz@123.com”
(在windows上执行时候,要在git bash中执行ssh-keygen,在cmd中可能无法执行,
遇到ssh-keygen不是内部或外部命令,则要在**/Git/usr/bin目录下找到ssh-keygen.exe,
将**/Git/usr/bin路径添加到环境变量中)
按3个回车,密码为空
Your identification has been saved in /home/tekkub/.ssh/id_rsa.
Your public key has been saved in /home/tekkub/.ssh/id_rsa.pub.
The key fingerprint is:
………………
得到两个文件
私钥: id_rsa
公钥:id_rsa.pub
4.在github或gitlab上添加公钥
添加多个git账号支持(例如同时使用github和gitlab)
1.生成新的ssh key
进入.ssh目录,通过命令ssh-keygen生成ssh key,
在执行命令后,不要回车,第一步要确认key的文件名,可以输入一个与之前不同的名字
例如 id_rsa_github,其后两步,皆回车,则会生成两个文件
私钥:id_rsa_github
公钥: id_rsa_github.pub
将公钥添加到github上
2.在.ssh下新建config文件,在其中添加配置
# gitlab
Host gitlab
HostName gitlab.com
User ygtzz
IdentityFile ~/.ssh/id_rsa //windows: IdentityFile C:\Users\xxx\.ssh\id_rsa
# github
Host github
HostName github.com
User ygtzz
IdentityFile ~/.ssh/id_rsa_github //windows: IdentityFile C:\Users\xxx\.ssh\id_rsa_github
注意:此处Host是HostName的别名,在git clone 地址的时候会使用host的来判别key,进行下载。例如:git@github.com/ygtzz/lazyload.git项目,在配置下,必须使用
git@{Host}/ygtzz/lazyload.git(即git@github/ygtzz/lazyload.git)地址去下载,git才能根据config找到对应的rsa文件。因此,建议Host和HostName保持一致,这样clone
时候就不用修改下载地址,直接可以下载。下面是一个支持github,gitlab,码云三个git端的配置文件(已验证可用):
#gitee
Host git.oschina.net
HostName git.oschina.net
User ygtzz
IdentityFile ~/.ssh/id_rsa_gitee #gitlab
Host git.xxx.com
Hostname git.xxx.com
User mengweif
IdentityFile ~/.ssh/id_rsa_gitlab #github
Host github.com
HostName github.com
User ygtzz
IdentityFile ~/.ssh/id_rsa
3.执行ssh-agent让ssh识别新的私钥
ssh-add ~/.ssh/id_rsa_new
该命令如果报错:Could not open a connection to your authentication agent.无法连接到ssh agent,则可执行ssh-agent bash命令后再执行ssh-add命令:
ssh-agent bash
ssh-add ~/.ssh/id_rsa_new
以后,在clone或者add remote的时候,需要把config文件中的host代替git@remoteaddress中的remoteaddress。
4.在git的工作目录中,设置本地的用户名和邮箱
$ git config --local user.name "github用户名"
$ git config --local user.email "注册邮箱"
如果不设置用户名,则能正常提交,但提交的用户名会是global设置的用户名
参考:https://my.oschina.net/csensix/blog/184434
https://www.jianshu.com/p/89cb26e5c3e8
最新文章
- java从基础知识(九)I/O
- 深入理解Loadrunner中的Browser Emulation
- 违反并发性: UpdateCommand影响了预期 1 条记录中的 0
- ACM题目————zoj问题
- HTTP协议中PUT/GET/POST/HEAD等介绍
- c 语言练习__求到N的阶乘的和。
- 修改xcode代码风格设置
- unity3d使用litjson中文显示的问题
- july教你如何迅速秒杀掉:99%的海量数据处理面试题
- Linux - 简明Shell编程02 - 变量(Variable)
- 【python】函数说明文档
- 阿里服务器CentOS报错base ls command not found
- 从零开始学安全(四十五)●browser_autopwn2漏洞利用配合Ettercap工具实施DNS欺骗攻击
- ASM
- python3中argparse模块
- ABP框架(asp.net core 2.X+Vue)模板项目学习之路(二)--切换MySql数据库
- 朱晔的互联网架构实践心得S1E5:不断耕耘的基础中间件
- Egret中使用P2物理引擎
- 利用NEST2.0 在C#中操作Elasticsearch
- sqlserver 中通配符%和_的使用