如何在一个系统上同时使用 gitee 和 github
<p>通过 ssh 密钥同时使用 gitee 和 github</p><h2 id="0-github-两种操作方式">0. github 两种操作方式</h2>
<ol>
<li>https<br>
可以随意克隆github上的项目,而不管是谁的;在pull/push的时候是需要验证用户名和密码。目前 github 更改了策略,不再使用密码,而是用 token 替代</li>
<li>ssh<br>
克隆者必须是拥者或管理员,且需要先添加 SSH key ,否则无法克隆。在pull/push的时候不再是验证用户名和密码,而是通过验证ssh的方式来验证用户。</li>
</ol>
<h2 id="1-清楚-git-全局配置">1. 清楚 git 全局配置</h2>
<p>如果之前没有配置过 git 全局配置可以无视以下操作,不确定可以查看下目前的全局配置</p>
<pre><code class="language-shell"># 查看目前全局配置
git config --global --list
# 删除之前配置的 user.name 和 user.email
git config --global --unset user.name "你的名字" //删除之前的名字配置
git config --global --unset user.email "你的邮箱"//删除之前的名字配置
</code></pre>
<h2 id="2-生成-ssh-keys">2. 生成 SSH keys</h2>
<p>许多 Git 服务器(包括但不限于gitee、github、gitlab)都使用 SSH 公钥进行认证。 为了向 Git 服务器提供 SSH 公钥,如果某系统用户尚未拥有密钥,必须事先为其生成一份。这个过程在所有操作系统上都是相似的。</p>
<p>首先需要确认自己是否已经拥有密钥。 默认情况下,用户的 SSH 密钥存储在其 ~/.ssh 目录下。</p>
<p>进入用户目录下的 .ssh 文件夹,如果没有此文件夹直接新建一个。</p>
<p>使用 ssh-keygen 生成密钥,参数含义:</p>
<ul>
<li>-t 指定密钥类型,默认是 rsa ,可以省略。</li>
<li>-C 设置注释文字,比如邮箱。</li>
<li>-f 指定密钥文件存储文件名。</li>
</ul>
<pre><code class="language-shell">cd ~/.ssh # windows cmd 不支持 ~
# gitee
ssh-keygen -t rsa -f id_rsa.gitee -C "youremail@xxx.com"
</code></pre>
<blockquote>
<p>gitee 会生成名为 id_rsa.gitee、id_rsa.gitee.pub 的文件,可以输入不同的名字来便于识别文件,比如生成 Gitee 的 ssh key。</p>
</blockquote>
<p>直接回车3下,就是默认没有密码</p>
<pre><code class="language-shell">Your identification has been saved in id_rsa.gitee.
Your public key has been saved in id_rsa.gitee.pub.
The key fingerprint is:
SHA256:F0K/ojCbFzgMPru11m4g/9uV03oHK+U0rKBLwOOye2c xxx@xxx.com
The key's randomart image is:
+-------+
| . |
| . . |
|. . o |
| . + . . o |
|o X . S o. |
|.+.O o.o o* |
|oo=o+. .+=.+ |
| =++E. .oo+ .|
|++.*=o. .o . |
+---------+
</code></pre>
<p>同理生成 github ssh keys</p>
<pre><code># github
ssh-keygen -t rsa -f id_rsa.github -C "youremail@xxx.com"
# 后续回车确认3次确认默认密码
</code></pre>
<h2 id="3-githubgitee-添加-public-key">3. github/gitee 添加 public key</h2>
<p>找到用户目录下的 .ssh 文件夹,打开刚生成的 id_rsa.gitee.pub 或 id_rsa.github.pub,将内容分别复制到 Gitee 和 Github 网站设置中的 ssh -> public key 中。</p>
<h2 id="4-创建配置文件">4. 创建配置文件</h2>
<p>在 .ssh 文件夹中创建 config 文件,添加以下内容以区分多个 ssh key:</p>
<pre><code># gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa.gitee
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa.github
#gitlab
Host 47.xxx.xxx.22x
HostName 47.xxx.xxx.xx4
User git
IdentityFile ~/.ssh/id_rsa.gitlab
</code></pre>
<blockquote>
<p>注意配置文件中可以根据需要增加代理,直接加在此文件对应位置即可,详情见博文 如何为 Git 设置代理</p>
</blockquote>
<h2 id="5-测试连接是否正常">5. 测试连接是否正常</h2>
<p>每个托管商的仓库都有唯一的后缀,比如 Github 的是 git@github.com</p>
<p>至此电脑端配置完毕,将两个ssh分别添加到github和gitee中,测试:</p>
<pre><code class="language-shell">ssh -T git@github.com
ssh -T git@gitee.com
</code></pre>
<p>如果返回类似于下面语句说明连接正常:</p>
<blockquote>
<p>You've successfully authenticated, but GitHub does not provide shell access.</p>
</blockquote>
<h2 id="6-官方文档">6. 官方文档</h2>
<ul>
<li>git 官方文档:服务器上的 Git - 生成 SSH 公钥</li>
<li>gitee官方文档</li>
<li>github官方文档</li>
</ul>
<p>下文摘自git官网</p>
<pre><code class="language-shell">$ ssh-keygen -o
Generating public/private rsa key pair.
Enter file in which to save the key (/home/schacon/.ssh/id_rsa):
Created directory '/home/schacon/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/schacon/.ssh/id_rsa.
Your public key has been saved in /home/schacon/.ssh/id_rsa.pub.
The key fingerprint is:
d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 schacon@mylaptop.local
</code></pre>
<blockquote>
<p>首先 ssh-keygen 会确认密钥的存储位置(默认是 .ssh/id_rsa),然后它会要求你输入两次密钥口令。 如果你不想在使用密钥时输入口令,将其留空即可。 然而,如果你使用了密码,那么请确保添加了 -o 选项,它会以比默认格式更能抗暴力破解的格式保存私钥。 你也可以用 ssh-agent 工具来避免每次都要输入密码。</p>
<p>现在,进行了上述操作的用户需要将各自的公钥发送给任意一个 Git 服务器管理员 (假设服务器正在使用基于公钥的 SSH 验证设置)。 他们所要做的就是复制各自的 .pub 文件内容,并将其通过邮件发送。</p>
</blockquote>
<h2 id="7-ssh-pushpull">7. ssh push/pull</h2>
<h2 id="参考文献">参考文献</h2>
<ul>
<li>使用ssh连接gitHub</li>
<li>同时使用Gitee(码云)和Github/Gitlab</li>
</ul><br><br>
来源:https://www.cnblogs.com/cscshi/p/16228905.html
頁:
[1]