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

最新文章

  1. java从基础知识(九)I/O
  2. 深入理解Loadrunner中的Browser Emulation
  3. 违反并发性: UpdateCommand影响了预期 1 条记录中的 0
  4. ACM题目————zoj问题
  5. HTTP协议中PUT/GET/POST/HEAD等介绍
  6. c 语言练习__求到N的阶乘的和。
  7. 修改xcode代码风格设置
  8. unity3d使用litjson中文显示的问题
  9. july教你如何迅速秒杀掉:99%的海量数据处理面试题
  10. Linux - 简明Shell编程02 - 变量(Variable)
  11. 【python】函数说明文档
  12. 阿里服务器CentOS报错base ls command not found
  13. 从零开始学安全(四十五)●browser_autopwn2漏洞利用配合Ettercap工具实施DNS欺骗攻击
  14. ASM
  15. python3中argparse模块
  16. ABP框架(asp.net core 2.X+Vue)模板项目学习之路(二)--切换MySql数据库
  17. 朱晔的互联网架构实践心得S1E5:不断耕耘的基础中间件
  18. Egret中使用P2物理引擎
  19. 利用NEST2.0 在C#中操作Elasticsearch
  20. sqlserver 中通配符%和_的使用

热门文章

  1. Qt listwigwt item 加入自定义元素
  2. 总结获取原生JS(javascript)基本操作
  3. Juice账号
  4. SpringBoot在IntelliJ IDEA下for MAC 热加载
  5. 使用 letter-space 后文字不能居中解决
  6. Thunder团队第六周 - Scrum会议1
  7. 软件工程第六周psp
  8. Android开发随笔5
  9. Khan Academy
  10. Windows 7中安装Solr7