配置同时使用 Gitlab、Github、Gitee(码云) 共存的开发环境
<h2 id="确认版本">确认版本</h2><p>首先确认已安装Git,可以通过 <code>git –version</code> 命令可以查看当前安装的版本。</p>
<p>可以通过命令 <code>git clone https://github.com/git/git</code> 进行更新</p>
<p>Git共有三个级别的config文件,分别是<code>system、global和local</code>。</p>
<p>在当前环境中,分别对应</p>
<p>%GitPath%\mingw64\etc\gitconfig文件<br>
$home.gitconfig文件<br>
%RepoPath%.git\config文件</p>
<p>其中<code>%GitPath%</code>为Git的安装路径,<code>%RepoPath%</code>为某仓库的本地路径。</p>
<p>所以 system 配置整个系统只有一个,global 配置每个账户只有一个,而 local 配置和git仓库的数目相同,并且只有在仓库目录才能看到该配置。</p>
<p><strong>建立两个密钥,不同账号配置不同的密钥,不同仓库配置不同密钥。</strong></p>
<h2 id="1-更改-git-的全局设置针对已安装-git">1. 更改 git 的全局设置(针对已安装 git)</h2>
<p>新安装 git 跳过。</p>
<p>若之前对 git 设置过全局的 <code>user.name</code> 和 <code>user.email</code>。<br>
用 <code>git config --global --list</code> 进行查看你是否设置</p>
<pre><code class="language-bash">git config --global user.name "chenfl"
git config --global user.email"1571539116@qq.com"
</code></pre>
<p>必须删除该设置</p>
<pre><code class="language-bash">git config --global --unset user.name "chenfl"
git config --global --unset user.email "1571539116@qq.com"
</code></pre>
<h2 id="2-生成新的-ssh-keys">2. 生成新的 SSH keys</h2>
<h3 id="1github-的钥匙">1)GitHub 的钥匙</h3>
<p>指定文件路径,方便后面操作:<code>~/.ssh/id_rsa.gitlab</code></p>
<pre><code class="language-bash">ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "1571539116@qq.com"
</code></pre>
<p>直接回车3下,什么也不要输入,就是默认没有密码。</p>
<p>注意 github 和 gitlab 和 gitee 的文件名是不同的。</p>
<p>2)GitLab 的钥匙</p>
<pre><code class="language-bash">ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "1571539116@qq.com"
</code></pre>
<h3 id="2gitee-的钥匙">2)Gitee 的钥匙</h3>
<pre><code class="language-bash">ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "1571539116@qq.com"
</code></pre>
<h3 id="3完成后会在ssh--目录下生成以下文件">3)完成后会在~/.ssh / 目录下生成以下文件</h3>
<ul>
<li>id_rsa.github</li>
<li>id_rsa.github.pub</li>
<li>id_rsa.gitlab</li>
<li>id_rsa.gitlab.pub</li>
</ul>
<hr>
<h2 id="3添加识别-ssh-keys-新的私钥">3.添加识别 SSH keys 新的私钥</h2>
<p>默认只读取 id_rsa,为了让 SSH 识别新的私钥,需要将新的私钥加入到 SSH agent 中</p>
<pre><code class="language-bash">ssh-agent bash
ssh-add ~/.ssh/id_rsa.github
ssh-add ~/.ssh/id_rsa.gitlab
ssh-add ~/.ssh/id_rsa.gitee
</code></pre>
<h2 id="4-多账号必须配置-config-文件重点">4. 多账号必须配置 config 文件(重点)</h2>
<p>若无 config 文件,则需创建 config 文件</p>
<h3 id="创建config文件">创建config文件</h3>
<pre><code class="language-bash">touch ~/.ssh/config
</code></pre>
<h3 id="config-里需要填的内容如下">config 里需要填的内容如下</h3>
<pre><code class="language-tsx">#Default gitHub user Self
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa.github
#Add gitLab user
Host git@gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa.gitlab
# gitee
Host gitee.com
Port 22
HostName gitee.com
User git
IdentityFile ~/.ssh/id_rsa.gitee
# 其他自己搭建的
Host git@git.startdt.net
Port 22
HostName http://git.startdt.net
User git
IdentityFile ~/.ssh/lab_rsa.startdt
</code></pre>
<p>下面对上述配置文件中使用到的配置字段信息进行简单解释:</p>
<ul>
<li>Host<br>
它涵盖了下面一个段的配置,我们可以通过他来替代将要连接的服务器地址。<br>
这里可以使用任意字段或通配符。<br>
当ssh的时候如果服务器地址能匹配上这里Host指定的值,则Host下面指定的HostName将被作为最终的服务器地址使用,并且将使用该Host字段下面配置的所有自定义配置来覆盖默认的/etc/ssh/ssh_config配置信息。</li>
<li>Port<br>
自定义的端口。默认为22,可不配置</li>
<li>User<br>
自定义的用户名,默认为git,可不配置</li>
<li>HostName<br>
真正连接的服务器地址</li>
<li>PreferredAuthentications<br>
指定优先使用哪种方式验证,支持密码和秘钥验证方式</li>
<li>IdentityFile<br>
指定本次连接使用的密钥文件</li>
</ul>
<h2 id="5在-github--gitlabgitee-网站添加-ssh">5.在 github 、 gitlab、gitee 网站添加 ssh</h2>
<h3 id="github">Github</h3>
<p><img src="https://img-blog.csdnimg.cn/b529d6ff03844a2ba14d69f870bc5fd8.jpeg" alt="在这里插入图片描述" loading="lazy"></p>
<p>Github 添加SSH公钥</p>
<p>直达地址:https://github.com/settings/keys</p>
<p>过程如下:</p>
<ol>
<li>登录 Github</li>
<li>点击右上方的头像,点击 <code>settings</code></li>
<li>选择 <code>SSH key</code></li>
<li>点击 <code>Add SSH key</code></li>
</ol>
<p>在出现的界面中填写 SSH key 的名称,填一个你自己喜欢的名称即可。<br>
将上面拷贝的<code>~/.ssh/id_rsa.xxx.pub</code><strong>文件全部内容</strong>粘帖到 key 一栏,在点击 “add key” 按钮就可以了。</p>
<p>添加过程 github 会提示你输入一次你的 github 密码 ,确认后即添加完毕。</p>
<h3 id="gitlab">Gitlab</h3>
<p><img src="https://img-blog.csdnimg.cn/d8ba0bfac64944a9b32a69ae25e31845.jpeg" alt="在这里插入图片描述" loading="lazy"></p>
<p>Gitlab添加SSH公钥</p>
<p>直达地址:https://gitlab.com/profile/keys</p>
<ol>
<li>登录 Gitlab</li>
<li>点击右上方的头像,点击 <code>settings</code></li>
<li>后续步骤如 Github</li>
</ol>
<h3 id="gitee--码云">Gitee / 码云</h3>
<p><img src="https://img-blog.csdnimg.cn/fb9b85bb065845008c95a2dbc7b27fea.jpeg" alt="在这里插入图片描述" loading="lazy"></p>
<p>码云 添加SSH公钥</p>
<p>直达地址:https://gitee.com/profile/sshkeys</p>
<ol>
<li>登录 Gitee</li>
<li>点击右上方的头像,点击 <code>设置</code></li>
<li>后续步骤如 Github</li>
</ol>
<p>添加过程 码云 会提示你输入一次你的 Gitee 密码 ,确认后即添加完毕。</p>
<h2 id="6测试是否连接成功">6.测试是否连接成功</h2>
<p>由于每个托管商的仓库都有唯一的后缀,比如 Github 的是 git@github.com😗。</p>
<p>所以可以这样测试:</p>
<pre><code class="language-bash">ssh -T git@github.com
ssh -T git@gitlab.com
ssh -T git@gitee.com
</code></pre>
<p>如果能看到一些 Welcome 信息,说明就是 OK 的了,如:</p>
<pre><code class="language-ruby">chenfl@DESKTOP-VFEC2HJ MINGW64 ~/.ssh
$ ssh -T git@github.com
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/)? yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
Hi cfl1! You've successfully authenticated, but GitHub does not provide shell access.
chenfl@DESKTOP-VFEC2HJ MINGW64 ~/.ssh
$ ssh -T git@gitee.com
The authenticity of host 'gitee.com (180.97.125.228)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no/)? yes
Warning: Permanently added 'gitee.com,180.97.125.228' (ECDSA) to the list of known hosts.
Hi chenfl! You've successfully authenticated, but GITEE.COM does not provide shell access.
</code></pre>
<p>结果如果出现这个就代表成功:</p>
<ul>
<li>GitHub -> successfully</li>
<li>GitLab -> Welcome to GitLab</li>
<li>Gitee -> successfully</li>
</ul>
<h2 id="7其他说明">7.其他说明</h2>
<p>有的时候github拉去速度十分慢可以使用国内镜像,目前已知Github国内镜像网站有github.com.cnpmjs.org和git.sdut.me/。速度根据各地情况而定,在clone某个项目的时候将github.com替换为github.com.cnpmjs.org即可。</p><br><br>
来源:https://www.cnblogs.com/chenfl/p/15657868.html
頁:
[1]