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. Kubernetes 集群升级docker版本

    Kubernetes 集群升级docker版本   原则:升级完一台正常后再接着升下一台. Work Node 一.迁移上的pod(保证业务,但期间会出现抖动) kubectl drain $NODE ...

  2. 【重大更新】AppWizard来了,emWin6.10版本来了

    说明: 1.快圣诞节了,MDK和SEGGER都太生猛了,发布了大量软件更新,而且都是比较大的改进,待我周报再给大家分享. 2.不枉我这么多年对emWin的支持,官方也用心,终于带来AppWizard, ...

  3. Flutter - You need to use a different version code for your APK or Android App Bundle because you already have one with version code 1.

    前两天提交了一个版本Google Play,结果今天收到拒绝的邮件,说App内购有问题. 于是把设置里面的支付宝和微信打赏功能关闭,又打了一个aab. 然后上传到Google Play,结果提示 Yo ...

  4. JS获取包含当前节点本身的代码内容(outerHtml)

    原生JS DOM的内置属性 outerHTML 可用来获取当前节点的html代码(包含当前节点),且此属性可使用jQuery的prop()获取 <div id="demo-test-0 ...

  5. onTouchEvent中,跟随手指滑动的view出现抖动

    在这次实践中,它抖动得不正常,太不正常,太抖. 其实是我代码上出现了问题,记录一下. 我是怎么设置滑动的呢? 通过改变view的margin. 然而我在onTouchEvent中怎么控制它滑动的大小呢 ...

  6. SVN服务器和客户端的下载和安装

    一.SVN服务器VisualSVN下载和安装 当前版本:4.1.3下载地址:https://www.visualsvn.com/server/download/下载下来的文件:VisualSVN-Se ...

  7. Python—解析HTML页面(HTMLParser)

    HTMLParser类的定义及常用方法 类的定义 HTMLParser主要是用来解析HTML文件(包括HTML中无效的标记). 参数convert_charrefs表示是否将所有的字符引用自动转化为U ...

  8. Ubuntu 16.04 安装 CUDA10.1 (解决循环登陆的问题)

    0. 前言 这里直接用 cuda安装文件同时安装 NVIDIA 驱动和 CUDA,没有单独安装更高版本的 NVIDIA 驱动: 此安装是在 Intel 集显下的图形化界面,即用集显做 display, ...

  9. SQLServer配置镜像,无法将 ALTER DATABASE 命令发送到远程服务器实例,数据库镜像配置未更改。请确保该服务器已连接,然后重试。

    问题描述: 在做镜像的时候镜像已经连接成功,但是见证服务器却报错 然后tcping了端口啥都是通的,后面发现原来是主机名称一样导致的问题 发现host文件里面: C:\Windows\System32 ...

  10. Consul-template+nginx实现自动负载均衡

    前言 consul-template 是 Consul 的一个守护程序,使用 consul-template 可以方便快速的实现对 Consul Key/Value 存储系统的访问,可以从 KV 系统 ...