别在我评论下面找存在感 發表於 2024-8-1 17:50:00

本地同时配置github 和gitee 远程仓库

<p>在当下,gitee 也成为国内很多开发人员交友社区。同时为了跟上时代的步伐,大家也不想放弃github。所以同时将自己的项目提交到gitee 和github 成了开发人员的诉求。</p>
<h2 id="git-全局用户设置">git 全局用户设置</h2>
<pre><code class="language-shell">## 产看 全局配置
git config --global --list

# 清除(如果未添加过,则不需要清除)
git config --global --unset user.name "name"
git config --global --unset user.email "@mail"


git config --global user.name "new name"                     
git config --global user.email "new emial"

# 注:--global 表示全局属性,所有的git项目都会共用属性。设置本地机器默认commit的昵称与Email. 必须使用在 gitee 或者 github 上配置的 email.
</code></pre>
<h2 id="生成生成新的-ssh-keys">生成生成新的 SSH keys</h2>
<h3 id="github-的钥匙">GitHub 的钥匙</h3>
<pre><code class="language-shell"># 第一步
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "邮箱1"

out:
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
# 说明: 遇到以上Enter passphrase (empty for no passphrase),直接敲回车即可,不需要输入用户名或者密码

# 第二步
一直敲回车
our identification has been saved in /Users/likun/.ssh/id_rsa.github.
Your public key has been saved in /Users/likun/.ssh/id_rsa.github.pub.
The key fingerprint is:
SHA256:xxxx xxxx
The key's randomart image is:
-------+
|   xx .   xxxx |
+---------+

</code></pre>
<h3 id="gitee-的钥匙">Gitee 的钥匙</h3>
<pre><code class="language-shell"># 第一步
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "邮箱2"

# 第二步
一直敲回车

Your identification has been saved in /Users/likun/.ssh/id_rsa.gitee.
Your public key has been saved in /Users/likun/.ssh/id_rsa.gitee.pub.
The key fingerprint is:
SHA256:dFuVYB3D7tIzMbioTmv1O5O1Jl4F8TLIpFqgk0RsHuo 1363653611@qq.com
The key's randomart image is:
+-------+
|   xx .   xxxx |
+---------+
</code></pre>
<h2 id="完成后会在ssh--目录下生成以下文件">完成后会在~/.ssh / 目录下生成以下文件。</h2>
<pre><code>- id_rsa.github
- id_rsa.github.pub
- id_rsa.gitee
- id_rsa.gitee.pub
</code></pre>
<h2 id="识别-ssh-keys-新的私钥">识别 SSH keys 新的私钥</h2>
<p>默认只读取 id_rsa,为了让 SSH 识别新的私钥,需要将新的私钥加入到 SSH agent 中。<strong>改操作可以不执行,不设置也可以成功。</strong></p>
<pre><code class="language-shell">ssh-agent bash
ssh-add ~/.ssh/id_rsa.github
ssh-add ~/.ssh/id_rsa.gitee
</code></pre>
<h2 id="多账号配置-config-文件">多账号配置 config 文件</h2>
<h3 id="创建config文件">创建config文件</h3>
<pre><code class="language-shell">touch ~/.ssh/config
</code></pre>
<h4 id="config-中填入如下内容">config 中填入如下内容</h4>
<pre><code class="language-shell">#Default gitHub user Self
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa.github

# gitee
Host gitee.com
    Port 22
    HostName gitee.com
    User git
    IdentityFile ~/.ssh/id_rsa.gitee
</code></pre>
<h2 id="添加-ssh">添加 ssh</h2>
<p>分别添加SSH到Gitee和Github:</p>
<ol>
<li>
<p>Github:<br>
https://github.com/settings/keys<br>
将 id_rsa.github.pub 中的内容填进去,起名的话随意。</p>
</li>
<li>
<p>Gitee:<br>
https://gitee.com/profile/sshkeys<br>
将 id_rsa.gitee.pub 中的内容填进去,起名的话随意。</p>
</li>
</ol>
<h2 id="测试成功">测试成功</h2>
<pre><code class="language-shell">ssh -T git@gitee.com
Hi zbcn! You've successfully authenticated, but GITEE.COM does not provide shell access.

ssh -T git@github.com
Hi 1363653611! You've successfully authenticated, but GitHub does not provide shell access.

</code></pre>
<h1 id="idea中同时push项目到gitee和github">IDEA中同时push项目到gitee和github</h1>
<ol>
<li>找到 git 的远程配置</li>
</ol>
<p><img src="https://img2024.cnblogs.com/blog/1040399/202408/1040399-20240801174747867-1809007902.png"></p>
<ol start="2">
<li>多个push项目的时候就可以切换我们想要push的地方</li>
</ol>
<p><img src="https://img2024.cnblogs.com/blog/1040399/202408/1040399-20240801174854826-1270812620.png"></p>
<h1 id="解决本地库同时关联github和gitee">解决本地库同时关联GitHub和Gitee</h1>
<h2 id="跳转到要添加关联远程仓库的项目下">跳转到要添加关联远程仓库的项目下</h2>
<p>我们在本地库上使用命令<code>git remote add</code>把它同时和Github、Gitee的远程库关联起来</p>
<pre><code class="language-shell">git remote add github git@github.com:xxx/xxx_test.git
git remote add gitee git@gitee.com:xxxx/xx-test.git

</code></pre>
<p><em>此处可以为https地址也可以是ssh地址,orign为设置的远程仓库的别名(如果我们关联两个的话,则需要设置不同名,比如github和gitee),<strong>强烈建议使用ssh方式</strong>,因为https方式每次都要输入用户名和密码</em></p>
<ul>
<li>关联完成后,我们可以通过输入<code>git remote -v</code>来查看关联的远程库信息</li>
</ul>
<h3 id="这样一来我们的本地库就可以同时与多个远程库互相同步">这样一来,我们的本地库就可以同时与多个远程库互相同步:</h3>
<p>如果要推送到GitHub,使用命令:<code>git push github master</code></p>
<p>如果要推送到Gitee,使用命令:<code>git push gitee master</code></p>
<h1 id="参考">参考</h1>
<ul>
<li>https://blog.csdn.net/Haa__/article/details/119353185</li>
<li>https://blog.csdn.net/Bertil/article/details/119464439</li>
<li>https://blog.csdn.net/qq_33442844/article/details/78491777</li>
</ul><br><br>
来源:https://www.cnblogs.com/zbcn/p/18337190
頁: [1]
查看完整版本: 本地同时配置github 和gitee 远程仓库