1.生成并部署SSH key

安装好Git客户端后,打开git bash,输入以下命令生成user1的SSH Key:
ssh-keygen -t rsa -C "user1@email.com"

在当前用户的.ssh目录下会生成id_rsa私钥文件和id_rsa.pub公钥文件,将id_rsa.pub中的内容添加至user1的github中。然后在git bash中输入以下命令测试该用户的SSH密钥是否生效:
ssh -T git@github.com

若连接成功则提示Hi user1! You've successfully authenticated, but GitHub does not provide shell access.
注:该命令仅限于文件名为id_rsa的密钥。

接着生成user2的密钥,注意不能再使用默认的文件名id_rsa,否则会覆盖之前密钥文件:

ssh-keygen -t rsa -f ~/.ssh/id_rsa2 -C "user2@email.com"

再将该用户的公钥文件添加至github中。
测试user2的ssh连接时需要指定密钥文件:

ssh -T git@github.com -i ~/.ssh/id_rsa2

也可以使用ssh agent添加密钥后进行测试。因为系统默认只读取id_rsa,为了让ssh识别新的私钥,可以使用ssh-agent手动添加私钥:

ssh-agent bash
ssh-add ~/.ssh/id_rsa2

注:该方法仅限当前窗口有效,打开新的窗口则ssh连接失败。

2.配置config文件

在.ssh目录下创建一个config文本文件,每个账号配置一个Host节点。主要配置项说明:

Host      主机别名
HostName  服务器真实地址
IdentityFile  私钥文件路径
PreferredAuthentications  认证方式
User  用户名

配置文件内容

# 配置user1
Host u1.github.com
HostName github.com
Port 6666
IdentityFile C:\\Users\\Administrator\\.ssh\\id_rsa
PreferredAuthentications publickey
User user1 # 配置user2
Host u2.github.com
HostName github.com
IdentityFile C:\\Users\\Administrator\\.ssh\\id_rsa2
PreferredAuthentications publickey
User user2

再通过终端测试SSH Key是否生效
ssh -T git@u1.github.com
ssh -T git@u2.github.com

3.配置用户名和邮箱

如果之前配置过全局的用户名和邮箱,需要取消相关配置,再在各仓库下配置相应的用户名和邮箱。

git config --global --unset user.name
git config --global --unset user.email

为各仓库单独配置用户名和邮箱

git config user.name "user1"
git config user.email "user1@email.com"

如果原先使用HTTPS通信,则需要修改远程仓库地址

git remote rm origin
git remote add origin git@u1.github.com:xxx/xxxxx.git

4.注意

ssh -T git@github 时,一定要打 yes!yes!yes!!! 我就是被这里坑了!

5.用法:

比如:github 项目地址(ssh):https://github.com/user1/xxxx.git

  • 正常用法,,要求输入登录密码:
    git clone https://github.com/user1/xxxx.git

  • 使用配置文件后,不需要输入密码:
    git clone git@github.com:user1/xxxx.git

  • 配置git push不用每次输入用户名和密码: git remote set-url origin git@xxx.com:xxx/xxx.git

6.参考文章

p{margin-top:0;margin-bottom:0}body .md-meta,body .md-before,body .md-after{color:#999}body table{margin-bottom:1.5em;font-size:1em}body thead th,body tfoot th{padding:.25em .25em .25em .4em;text-transform:uppercase}body th{text-align:left}body td{vertical-align:top;padding:.25em .25em .25em .4em}body code,body .md-fences{color:inherit;background-color:#dadada;padding-left:1ch;padding-right:1ch}body pre code{background-color:inherit}body .md-fences{margin-left:2em;margin-bottom:3em}body .md-fences .CodeMirror.CodeMirror-wrap{top:-0.8em}body pre,body code,body tt{font-size:.875em;line-height:1.714285em}body h1{line-height:1.3em;font-weight:normal;margin-bottom:.5em}body p+ul,body p+ol{margin-top:-1em}body h3+ul,body h4+ul,body h5+ul,body h6+ul,body h3+ol,body h4+ol,body h5+ol,body h6+ol{margin-top:.5em}body li>ul,body li>ol{margin-top:inherit}body h2,body h3{margin-bottom:.75em}body hr{border-top:none;border-right:none;border-bottom:1px solid;border-left:none}body h1{border-color:#c5c5c5}body blockquote{border-color:#bababa;color:#656565}body thead.md-table-edit{background-color:transparent}body thead{background-color:#dadada}body tr:nth-child(even){background:#e8e7e7}body hr{border-color:#c5c5c5}body .task-list{padding-left:1rem}body .task-list-item{list-style-type:none;margin-left:-1.8em}body .task-list-item .task-list-item-checkbox{margin-right:.8em}body .task-list-item input:before{content:'\221A';display:inline-block;width:1.25rem;height:1.5rem;vertical-align:middle;text-align:center;color:#ddd;background-color:#F3F2EE}body .task-list-item input:checked:before,body .task-list-item input[checked]:before{color:inherit}body img{max-width:100%}.markdown-preview{width:100%;height:100%;box-sizing:border-box}.markdown-preview .pagebreak,.markdown-preview .newpage{page-break-before:always}.markdown-preview pre.line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}.markdown-preview pre.line-numbers>code{position:relative}.markdown-preview pre.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:1em;font-size:100%;left:0;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.markdown-preview pre.line-numbers .line-numbers-rows>span{pointer-events:none;display:block;counter-increment:linenumber}.markdown-preview pre.line-numbers .line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}.markdown-preview .mathjax-exps .MathJax_Display{text-align:center !important}.markdown-preview:not([for="preview"]) .code-chunk .btn-group{display:none}.markdown-preview:not([for="preview"]) .code-chunk .status{display:none}.markdown-preview:not([for="preview"]) .code-chunk .output-div{margin-bottom:16px}.scrollbar-style::-webkit-scrollbar{width:8px}.scrollbar-style::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}.scrollbar-style::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,0.66);border:4px solid rgba(150,150,150,0.66);background-clip:content-box}html body[for="html-export"]:not([data-presentation-mode]){position:relative;width:100%;height:100%;top:0;left:0;margin:0;padding:0;overflow:auto}html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{position:relative;top:0}@media screen and (min-width:914px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{padding:2em calc(50% - 457px + 2em)}}@media screen and (max-width:914px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{font-size:14px !important;padding:1em}}@media print{html body[for="html-export"]:not([data-presentation-mode]) #sidebar-toc-btn{display:none}}html body[for="html-export"]:not([data-presentation-mode]) #sidebar-toc-btn{position:fixed;bottom:8px;left:8px;font-size:28px;cursor:pointer;color:inherit;z-index:99;width:32px;text-align:center;opacity:.4}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] #sidebar-toc-btn{opacity:1}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc{position:fixed;top:0;left:0;width:300px;height:100%;padding:32px 0 48px 0;font-size:14px;box-shadow:0 0 4px rgba(150,150,150,0.33);box-sizing:border-box;overflow:auto;background-color:inherit}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar{width:8px}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,0.66);border:4px solid rgba(150,150,150,0.66);background-clip:content-box}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc a{text-decoration:none}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc ul{padding:0 1.6em;margin-top:.8em}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc li{margin-bottom:.8em}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc ul{list-style-type:none}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{left:300px;width:calc(100% - 300px);padding:2em calc(50% - 457px - 150px);margin:0;box-sizing:border-box}@media screen and (max-width:1274px){html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{width:100%}}html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .markdown-preview{left:50%;transform:translateX(-50%)}html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .md-sidebar-toc{display:none}
/* Please visit the URL below for more information: */
/* https://shd101wyy.github.io/markdown-preview-enhanced/#/customize-css */
-->

在Windows下配置多个git账号的更多相关文章

  1. Windows下配置多个Git账号

    1. 为什么会用多个git账号? 不同git账号对应不同代码托管平台,如:github.bitbucket.gitlab.gitee(码云)等 2个GitHub账号,用于测试(最近需求,之前没研究过g ...

  2. Windows下配置Git多账号github码云

    Windows下配置Git多账号github码云 1.配置了全局用户名和邮箱 $ git config --global user.email "你的邮箱" $ git confi ...

  3. windows下使用TortoiseGit代替Git命令行操作

    windows下使用TortoiseGit代替Git命令行操作 大家在使用svn的时候,都非常喜欢使用小乌龟,也就是TortoiseSVN:那么git也有小乌龟版本,即TortoiseGit. 1.安 ...

  4. 配置多个 git 账号的 ssh密钥

    背景 在工作中,我们通常会以 ssh 的方式配置公司的 git 账号,但是平时也会使用 github 管理自己的项目.因此,我们需要为自己的 github 创建一个新的 git 账号,这就需要生成新的 ...

  5. 配置多个git账号的ssh密钥

    博客改版,请直接访问新版文章:https://www.cnblogs.com/xiaoxi666/p/9975981.html 背景 我们在工作中会以 ssh 的方式配置公司的 git 账号,但是平时 ...

  6. windows下配置ssh访问github

    一.说明 一直使用HTTPS的方式访问github的代码,用的时间长了,发现这是效率很低的一种开发行为,因为每次git push的时候都要输入username和password.今天就介绍如何在win ...

  7. Windows 下配置 Vagrant 环境

    Vagrant是一个基于 Ruby 的工具,用于创建和部署虚拟化开发环境.它使用 Oracle 的开源VirtualBox虚拟化系统. Vagrant 在快速搭建开发环境方面是很赞的,试想一个团队中, ...

  8. 在Windows下配置Linux远程开发环境

    在Windows下配置Linux远程开发环境 欢迎光临我的个人博客 https://source.chens.life/Configure-Linux-remote-development-envir ...

  9. windows 下配置 Nginx 常见问题(转)

    windows 下配置 Nginx 常见问题 因为最近的项目需要用到负载均衡,不用考虑,当然用大名鼎鼎的Nginx啦.至于Nginx的介绍,这里就不多说了,直接进入主题如何在Windows下配置. 我 ...

随机推荐

  1. 解压小游戏之打砖块(C#+unity)

    z这个小游戏很简洁,很简单,非常适合一个人玩,特别减压

  2. 流分析 Stream Analytics-实时数据流式处理,可处理来自数百万台 IoT 设备的数据

    典型的物联网架构中,有实时数据分析的需求,在Azure中,流分析(stream analytics)就是这样的服务,它可以存在云中或者部署到边缘设备上. 流分析的基本概念: https://v.qq. ...

  3. Vue学习笔记Day1

    1.vue初时 vue安装三种方式: 1:CDN引入 以下推荐国外比较稳定的两个 CDN,国内还没发现哪一家比较好,目前还是建议下载到本地. Staticfile CDN(国内) : https:// ...

  4. Sqlite—删除语句(Delete)

    SQLite 的 DELETE 语句用于删除表中已有的记录.可以使用带有 WHERE 子句的 DELETE 查询来删除选定行,否则所有的记录都会被删除. SQLite 要清空表记录,只能使用Delet ...

  5. 如何在windows下安装linux双系统

    首先是看这篇博客,讲得很详细,但是有一点小小的区别,这里把整个过程回顾一下. https://www.cnblogs.com/masbay/p/10745170.html 第一步,刻盘,将一个u盘刻录 ...

  6. MQ报错2009/2085解决方法

    1.1. 响应2009错误 1.1.1.   涉及协议 MQ,调试回放阶段 1.1.2.   错误信息 完成码2原因为2009 1.1.3.   可能原因 远端MQ连接数不足,拒绝连接 1.1.4.  ...

  7. Asp.Net MVC Webapi Eventsource 实现时效消息

    ApiController 后端API public System.Net.Http.HttpResponseMessage GetUnknownNotices() { var user = Form ...

  8. 传统js和jsx与ts和tsx的区别

    一.从定义文件格式方面说 1.传统的开发模式可以定义js文件或者jsx文件2.利用ts开发定义的文件格式tsx二.定义state的状态来说 1.传统的方式直接在构造函数中使用 constructor( ...

  9. 在Ubuntu 18.04系统上安装Pydio Cells详细图文教程

    前言   基于云的协作工具Pydio cell提供了一系列灵活的特性,包括应用内消息传递.文件共享和版本控制.下面逐步介绍安装过程. Pydio cell最初是一个简单的基于云的文件共享系统,但经过升 ...

  10. 某酒店建筑设计CAD施工图

    本素材为某酒店建筑设计CAD施工图,其中包涵的有酒店室内装修图纸.各个标间房屋改造图以及酒店场外建筑施工图.其中图纸的格式都是为dwg格式的.想要查看图纸就可以使用CAD看图软件来进行查看.以下就是一 ...