输过败过不曾怕过 發表於 2024-5-11 17:59:00

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

<h1 id="使用git-bash切换giteegithub多个git账号">使用Git bash切换Gitee、GitHub多个Git账号</h1>
<p>​                Git是分布式代码管理工具,使用命令行的方式提交commit、revert回滚代码。这里介绍使用Git bash软件来切换Gitee、GitHub账号。<br>
​                假设在gitee.com上的邮箱是alice@foxmail.com 、用户名为alice;在github上的邮箱是bob@foxmail.com、用户名为bob。称邮箱网站1alicealice@foxmail.comhttp://gitee.com2bobbob@foxmail.comhttp://github.com</p>
<table>
<thead>
<tr>
<th>账号</th>
<th>名称</th>
<th style="text-align: center">邮箱</th>
<th>网站</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>alice</td>
<td style="text-align: center">alice@foxmail.com</td>
<td>http://gitee.com</td>
</tr>
<tr>
<td>2</td>
<td>bob</td>
<td style="text-align: center">bob@foxmail.com</td>
<td>http://github.com</td>
</tr>
</tbody>
</table>
<h3 id="1安装git软件">1、安装Git软件</h3>
<p>​                官网地址:https://git-scm.com/downloads</p>
<p>​                一路默认,直到安装完成。</p>
<h3 id="2通过邮箱生成公私钥">2、通过邮箱生成公私钥</h3>
<p>​                使用管理员身份打开Git bash软件,然后根据邮箱来生成rsa公钥、私钥文件,命令如下:</p>
<center><img src="https://gitee.com/Gao-by/blog-park-essay-pictures/raw/master/imgs/0288299144994b068c5c3abdf2a06a10.png"></center>
<center>图(1) 使用管理员身份,运行Git Bash</center>
<p>​                通过ssh-keygen命令,来生成邮箱对应的公钥、私钥文件:</p>
<pre><code class="language-cmd">## 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"
</code></pre>
<p>​                在C:\用户\XXX.ssh里,可以看到有4个文件,如图(2) 所示:</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>文件名</th>
<th>含义</th>
<th style="text-align: center">所属账号</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>id_rsa_gitee</td>
<td>alice的私钥文件</td>
<td style="text-align: center">alice@foxmail.com</td>
</tr>
<tr>
<td>2</td>
<td>id_rsa_gitee.pub</td>
<td>alice的公钥文件</td>
<td style="text-align: center">alice@foxmail.com</td>
</tr>
<tr>
<td>3</td>
<td>id_rsa_github</td>
<td>bob的私钥文件</td>
<td style="text-align: center">bob@foxmail.com</td>
</tr>
<tr>
<td>4</td>
<td>id_rsa_github.pub</td>
<td>bob的公钥文件</td>
<td style="text-align: center">bob@foxmail.com</td>
</tr>
</tbody>
</table>
<center><img src="https://gitee.com/Gao-by/blog-park-essay-pictures/raw/master/imgs/9d60beb644ed4f658f6ae5b86a8e1450.png"></center>
<center>图(2) 以.pub结尾是公钥文件,非.pub结尾是私钥文件</center>
<h3 id="3将公钥设置到网站的ssh框框里">3、将公钥设置到网站的SSH框框里</h3>
<h4 id="31-设置alice在gitee里的ssh公钥">3.1 设置alice在gitee里的SSH公钥</h4>
<p>​                在gitee的账号 --》设置 --》安全设置 --》SSH公钥 --》拷贝id_rsa_gitee.pub文件里的信息到公钥输入框,然后点击【确定】即可,如图(5)所示</p>
<pre><code class="language-cmd">https://gitee.com/profile/sshkeys
</code></pre>
<center><img src="https://gitee.com/Gao-by/blog-park-essay-pictures/raw/master/imgs/c0cd681c9e594519aafb07382e258654.png"></center>
<center>图(3) 将生成的公钥id_rsa_gitee.pub内容,拷贝到gitee账号的SSH里</center>
<h4 id="32生成bob在githubcom上的公私钥文件">3.2生成bob在github.com上的公私钥文件</h4>
<p>​                在github的账号 --》Setting --》SSH and GPG Keys --》SSH公钥 --》New SSH --》拷贝id_rsa_github.pub文件里的信息到公钥输入框,然后点击【确定】即可,如图(4)、图(5)所示。</p>
<pre><code class="language-cmd">https://github.com/settings/keys
</code></pre>
<center><img src="https://gitee.com/Gao-by/blog-park-essay-pictures/raw/master/imgs/af3ba0882d284836836f0efbbfebc94a.png"></center>
<center>图(4) 点击Setting --》SSH and GPG keys --》New SSH</center>
<center><img src="https://gitee.com/Gao-by/blog-park-essay-pictures/raw/master/imgs/146ecbccabe642b1a0ca62a662ba7732.png"></center>
<center>图(5) 将生成的公钥id_rsa_github.pub内容,拷贝到github账号的SSH里</center>
<h3 id="4-在git-bash里激活公钥并授权">4 、在Git Bash里激活公钥,并授权</h3>
<p>​                在Git Bash黑框框里,激活公钥,并授权访问gitee或github。</p>
<h4 id="41-激活并授权gitee账号">4.1 激活并授权gitee账号</h4>
<p>​                激活命令(或测试命令)、授权命令gitee,如图(6)所示:</p>
<pre><code class="language-cmd">## 1) 激活公钥
ssh -T git@gitee.com -i ~/.ssh/id_rsa_gitee

## 2)授权
yes
</code></pre>
<center><img src="https://gitee.com/Gao-by/blog-park-essay-pictures/raw/master/imgs/b663820fa8954cb798c9531e007facb1.png"></center>
<center>图(6) 激活并授权gitee账号</center>
<h4 id="42-激活并授权github账号">4.2 激活并授权github账号</h4>
<p>​                激活命令(或测试命令)、授权命令github,如图(7)所示:</p>
<pre><code class="language-cmd">## 1) 激活公钥
ssh -T git@github.com -i ~/.ssh/id_rsa_github

## 2) 授权
yes
</code></pre>
<center><img src="https://gitee.com/Gao-by/blog-park-essay-pictures/raw/master/imgs/fa7f415013314cd0afb0fe392d28f448.png"></center>
<center>图(7) 激活并授权github账号 </center>
<h3 id="5-将私钥文件添加到git">5 、将私钥文件添加到git</h3>
<h4 id="51-将gitee的私钥文件添加到git">5.1 将gitee的私钥文件添加到git</h4>
<p>​                gitee的私钥文件是id_rsa_gitee,通过ssh-add添加到git的命令如下:</p>
<pre><code class="language-cmd">## 添加到git
ssh-add ~/.ssh/id_rsa_gitee
</code></pre>
<h4 id="52-将github的私钥文件添加到git">5.2 将github的私钥文件添加到git</h4>
<p>​                github的私钥文件是id_rsa_github,通过ssh-add添加到git的命令如下:</p>
<pre><code class="language-cmd">## 添加到git
ssh-add ~/.ssh/id_rsa_github
</code></pre>
<h3 id="6-配置config文件">6 、配置config文件</h3>
<p>​                config文件,一般保存在~/.ssh/目录里,用于切换多个gitee、github账号。</p>
<h4 id="61-创建config文件">6.1 创建config文件</h4>
<pre><code class="language-cmd">## 创建config文件
touch ~/.ssh/config
</code></pre>
<h4 id="62-填写要切换的账号和网站">6.2 填写要切换的账号和网站</h4>
<p>​                文件~/.ssh/config的内容如下:</p>
<pre><code class="language-cmd">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
</code></pre>
<p>​                其中,useEE对应alice, useHub对应bob。</p>
<p>​                就可以正常使用git命令推送到远程仓库等操作,例如:</p>
<pre><code>git add .
git commit -am "test"
git push
</code></pre>
<h3 id="7常见报错情况">7、常见报错情况</h3>
<p>​                在使用Git将本地仓库推送到远程仓库或将远程仓库克隆到本地的时候,发生了如下错误:“fatal: Could not read from remote repository.”</p>
<center><img src="https://gitee.com/Gao-by/blog-park-essay-pictures/raw/master/imgs/b938ecddcb22429cb6416cebab4f63d7.png"></center>
<p><strong>解决办法:</strong></p>
<ol>
<li>使用步骤4重新激活(每次使用都需要激活)</li>
<li>使用步骤5重新添加</li>
</ol>
<h4 id="71如果出现could-not-open-a-connection-to-your-authentication-agent的错误">7.1如果出现“Could not open a connection to your authentication agent.”的错误</h4>
<center><img src="https://gitee.com/Gao-by/blog-park-essay-pictures/raw/master/imgs/05ad48504f0e40de9ce7c00ce5727d54.png"></center>
<p><strong>使用如下命令解决:</strong></p>
<pre><code class="language-cmd">eval `ssh-agent`
</code></pre>
<p>然后再次执行<strong>步骤5</strong>,之后就可以正常使用git命令推送到远程仓库或下拉仓库等操作了</p><br><br>
来源:https://www.cnblogs.com/gaogao-web/p/18186974
頁: [1]
查看完整版本: 使用Git bash切换Gitee、GitHub多个Git账号