Loading

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

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

​ Git是分布式代码管理工具,使用命令行的方式提交commit、revert回滚代码。这里介绍使用Git bash软件来切换Gitee、GitHub账号。
​ 假设在gitee.com上的邮箱是alice@foxmail.com 、用户名为alice;在github上的邮箱是bob@foxmail.com、用户名为bob。称邮箱网站1alicealice@foxmail.comhttp://gitee.com2bobbob@foxmail.comhttp://github.com

账号 名称 邮箱 网站
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命令,来生成邮箱对应的公钥、私钥文件:

## 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)所示

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

3.2生成bob在github.com上的公私钥文件

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

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)所示:

## 1) 激活公钥
ssh -T git@gitee.com -i ~/.ssh/id_rsa_gitee

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

4.2 激活并授权github账号

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

## 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的命令如下:

## 添加到git
ssh-add ~/.ssh/id_rsa_gitee

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

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

## 添加到git
ssh-add ~/.ssh/id_rsa_github

6 、配置config文件

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

6.1 创建config文件

## 创建config文件
touch ~/.ssh/config

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

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

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。

​ 就可以正常使用git命令推送到远程仓库等操作,例如:

git add .
git commit -am "test"
git push

7、常见报错情况

​ 在使用Git将本地仓库推送到远程仓库或将远程仓库克隆到本地的时候,发生了如下错误:“fatal: Could not read from remote repository.”

解决办法:

  1. 使用步骤4重新激活(每次使用都需要激活)
  2. 使用步骤5重新添加

7.1如果出现“Could not open a connection to your authentication agent.”的错误

使用如下命令解决:

eval `ssh-agent`

然后再次执行步骤5,之后就可以正常使用git命令推送到远程仓库或下拉仓库等操作了

posted @ 2024-05-11 17:59  gaogao~  阅读(17)  评论(0编辑  收藏  举报