使用Git bash切换Gitee、GitHub多个Git账号

Git是分布式代码管理工具,使用命令行的方式提交commit、revert回滚代码。这里介绍使用Git bash软件来切换Gitee、GitHub账号。

假设在gitee.com上的邮箱是alice@foxmail.com 、用户名为alice;在github上的邮箱是bob@foxmail.com、用户名为bob。

账号 名称 邮箱 网站
1 alice alice@foxmail.com http://gitee.com
2 bob bob@foxmail.com http://github.com

1 安装Git软件

官网地址: https://git-scm.com/downloads

一路默认,直到安装完成。

2 通过邮箱生成公私钥

使用管理员身份打开Git bash软件,然后根据邮箱来生成rsa公钥、私钥文件,命令如下:

图(1) 使用管理员身份,运行Git Bash

通过ssh-keygen命令,来生成邮箱对应的公钥、私钥文件:

html 复制代码
## 1) 生成alice在gitee.com上的公私钥文件
ssh-keygen -t rsa -f ~/.ssh/id_rsa_gitee -C "alice@foxmail.com" 

## 2)生成bob在github.com上的公私钥文件
ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C "bob@foxmail.com" 

在C:\用户\XXX\.ssh里,可以看到有4个文件,如图(2) 所示:

序号 文件名 含义 所属账号
1 id_rsa_gitee alice的私钥文件 alice@foxmail.com
2 id_rsa_gitee.pub alice的公钥文件 alice@foxmail.com
3 id_rsa_github bob的私钥文件 bob@foxmail.com
4 id_rsa_github.pub bob的公钥文件 bob@foxmail.com


图(2) 以.pub结尾是公钥文件,非.pub结尾是私钥文件

3 将公钥设置到网站的SSH框框里

3.1 设置alice在gitee里的SSH公钥

在gitee的账号 --》设置 --》安全设置 --》SSH公钥 --》拷贝id_rsa_gitee.pub文件里的信息到公钥输入框,然后点击【确定】即可,如图(5)所示。

html 复制代码
https://gitee.com/profile/sshkeys


图(3) 将生成的公钥id_rsa_gitee.pub内容,拷贝到gitee账号的SSH里

3.2 设置bob在github里的SSH公钥

在github的账号 --》Setting --》SSH and GPG Keys --》SSH公钥 --》New SSH --》拷贝id_rsa_github.pub文件里的信息到公钥输入框,然后点击【确定】即可,如图(4)、图(5)所示。

html 复制代码
https://github.com/settings/keys


图(4) 点击Setting --》SSH and GPG keys --》New SSH

图(5) 将生成的公钥id_rsa_github.pub内容,拷贝到github账号的SSH里

4 在Git Bash里激活公钥,并授权

在Git Bash黑框框里,激活公钥,并授权访问gitee或github。

4.1 激活并授权gitee账号

激活命令(或测试命令)、授权命令gitee,如图(6)所示:

html 复制代码
## 1) 激活公钥
ssh -T git@gitee.com -i ~/.ssh/id_rsa_gitee

## 2)授权
yes


图(6) 激活并授权gitee账号

4.2 激活并授权github账号

激活命令(或测试命令)、授权命令github,如图(7)所示:

html 复制代码
## 1) 激活公钥
ssh -T git@github.com -i ~/.ssh/id_rsa_github

## 2) 授权
yes


图(7) 激活并授权github账号

5 将私钥文件添加到git

5.1 将gitee的私钥文件添加到git

gitee的私钥文件是id_rsa_gitee,通过ssh-add添加到git的命令如下:

html 复制代码
## 添加到git
ssh-add ~/.ssh/id_rsa_gitee

5.2 将github的私钥文件添加到git

github的私钥文件是id_rsa_github,通过ssh-add添加到git的命令如下:

html 复制代码
## 添加到git
ssh-add ~/.ssh/id_rsa_github

6 配置config文件

config文件,一般保存在~/.ssh/目录里,用于切换多个gitee、github账号。

6.1 创建config文件

html 复制代码
## 创建config文件
touch ~/.ssh/config

6.2 填写要切换的账号和网站

文件~/.ssh/config的内容如下:

bash 复制代码
Host useEE
HostName gitee.com
IdentityFile C:\\Users\\25014\\.ssh\\id_rsa_gitee
PreferredAuthentications publickey
User useEE


Host useHub
HostName github.com
IdentityFile C:\\Users\\25014\\.ssh\\id_rsa_github
PreferredAuthentications publickey
User useHub

其中,useEE对应alice, useHub对应bob。

7 拉取工程

7.1 拉取gitee上的timer工程

假设原来的timer工程地址为:git@gitee.com:alice/timer.git,Git软件则通过~/.ssh/config文件重新设置名称之后,其有效的地址如下:

html 复制代码
git@useEE:alice/timer.git

则拉取timer工程的命令:

html 复制代码
git clone git@gitee.com:alice/timer.git

等价改成,如下:

html 复制代码
git clone git@useEE:alice/timer.git

7.2 拉取github上的timer工程

同理,假设原来的timer工程地址为:git@github.com:bob/timer.git,则通过config重新设置名称之后,其有效的地址如下:

html 复制代码
git@useHub:bob/timer.git

则拉取timer工程的命令:

html 复制代码
git clone git@github.com:bob/timer.git

等价改成,如下:

html 复制代码
git clone git@useHub:bob/timer.git
相关推荐
aFakeProgramer5 小时前
VRTE 的应用程序部署到Ubuntu上 报错:bash: ./rb_exmd: No such file or directory
bash
菜鸟也会Fly5 小时前
【/usr/bin/env: “bash\r”: 没有那个文件或目录】问题解决
linux·bash
wdfk_prog1 天前
实战教程:从“对象文件为空“到仓库重生——修复 Git 仓库损坏全记录
大数据·网络·笔记·git·学习·elasticsearch·全文检索
ALex_zry1 天前
Git Status 命令深度指南:洞悉仓库状态的核心艺术
大数据·git·elasticsearch
啃火龙果的兔子1 天前
如何在 VS Code 中进行 `cherry-pick`
git
夜里慢慢行4561 天前
git工程多个remote 拉取推送
git
啃火龙果的兔子1 天前
Git `cherry-pick` 工具汇总
git
似霰2 天前
安卓系统属性之androidboot.xxx转换成ro.boot.xxx
android·gitee
ALex_zry2 天前
Git 乱码文件处理全流程指南:从识别到彻底清除
git·elasticsearch·搜索引擎
李梦晓2 天前
git 提交代码到别的分支
前端·git