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

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

假设在gitee.com上的邮箱是[email protected] 、用户名为alice;在github上的邮箱是[email protected]、用户名为bob。

账号 名称 邮箱 网站
1 alice [email protected] http://gitee.com
2 bob [email protected] 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 "[email protected]" 

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

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

序号 文件名 含义 所属账号
1 id_rsa_gitee alice的私钥文件 [email protected]
2 id_rsa_gitee.pub alice的公钥文件 [email protected]
3 id_rsa_github bob的私钥文件 [email protected]
4 id_rsa_github.pub bob的公钥文件 [email protected]


图(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 [email protected] -i ~/.ssh/id_rsa_gitee

## 2)授权
yes


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

4.2 激活并授权github账号

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

html 复制代码
## 1) 激活公钥
ssh -T [email protected] -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工程地址为:[email protected]:alice/timer.git,Git软件则通过~/.ssh/config文件重新设置名称之后,其有效的地址如下:

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

则拉取timer工程的命令:

html 复制代码
git clone [email protected]:alice/timer.git

等价改成,如下:

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

7.2 拉取github上的timer工程

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

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

则拉取timer工程的命令:

html 复制代码
git clone [email protected]:bob/timer.git

等价改成,如下:

html 复制代码
git clone git@useHub:bob/timer.git
相关推荐
Cchaofan2 小时前
Git/GitLab日常使用的命令指南来了!
git·gitlab
不念霉运2 小时前
Gitee DevOps:中国企业数字化转型的“本土化加速器“
运维·gitee·团队开发·代码规范·devops·代码复审
ddd...e_bug6 小时前
Shell和Bash介绍
开发语言·bash
難釋懷8 小时前
Android开发-文本输入
android·gitee
可乐加.糖9 小时前
项目版本管理和Git分支管理方案
java·git·目标跟踪·gitlab·敏捷流程·源代码管理
wingaso9 小时前
[经验总结]删除gitlab仓库分支报错:错误:无法推送一些引用到“http:”
linux·数据仓库·git
妮妮分享14 小时前
维智定位 Android 定位 SDK
gitee
ice___Cpu18 小时前
Git - 1( 14000 字详解 )
大数据·git·elasticsearch
范纹杉想快点毕业1 天前
以项目的方式学QT开发(一)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
c语言·数据结构·c++·git·qt·链表·github
ddd...e_bug1 天前
GMT之Bash语言使用
开发语言·bash