同时使用gitee和github
<p>https://juejin.im/post/6844904180633600007</p><p>https://www.jianshu.com/p/68578d52470c</p>
<p>Git共有三个级别的config文件,分别是:</p>
<ul>
<li><strong>system</strong> :%GitPath%\mingw64\etc\gitconfig文件</li>
<li><strong>global</strong>:$home.gitconfig文件</li>
<li><strong>local</strong>:%RepoPath%.git\config文件</li>
</ul>
<p>其中<code>%GitPath%</code>为Git的安装路径,<code>%RepoPath%</code>为某仓库的本地路径。</p>
<h1 id="1-删掉全局配置">1. 删掉全局配置</h1>
<pre><code class="language-git">git config --global --list
$ git config --global --unset user.name "你的名字"
$ git config --global --unset user.email "你的邮箱"
</code></pre>
<h1 id="2-为不同账户配置ssh秘钥">2. 为不同账户配置ssh秘钥</h1>
<pre><code>cd ~/.ssh # cd到当前用户的.ssh文件夹
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "注册gitee邮箱" #为不同秘钥指定名称
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "注册github邮箱"
</code></pre>
<p>完成后会在~/.ssh / 目录下生成以下文件:</p>
<ul>
<li>id_rsa.github</li>
<li>id_rsa.github.pub</li>
<li>id_rsa.gitee</li>
<li>id_rsa.gitee.pub</li>
</ul>
<p>复制公钥分别在github和gitee中设置</p>
<pre><code>cat id_rsa.github.pub
cat id_rsa.gitee.pub
</code></pre>
<p>添加新的私钥</p>
<pre><code>$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa.github
$ ssh-add ~/.ssh/id_rsa.gitee
</code></pre>
<h1 id="3-进行全局配置">3. 进行全局配置</h1>
<pre><code>touch ~/.ssh/config
# 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
</code></pre>
<p>Host 它涵盖了下面一个段的配置,我们可以通过他来替代将要连接的服务器地址。 这里可以使用任意字段或通配符。 当ssh的时候如果服务器地址能匹配上这里Host指定的值,则Host下面指定的HostName将被作为最终的服务器地址使用,并且将使用该Host字段下面配置的所有自定义配置来覆盖默认的/etc/ssh/ssh_config配置信息。</p>
<p>Port 自定义的端口。默认为22,可不配置</p>
<p>User 自定义的用户名,默认为git,可不配置</p>
<p>HostName 真正连接的服务器地址</p>
<p>PreferredAuthentications 指定优先使用哪种方式验证,支持密码和秘钥验证方式</p>
<p>IdentityFile 指定本次连接使用的密钥文件</p>
<h1 id="4-测试连接">4. 测试连接</h1>
<pre><code>ssh -T git@github.com
Hi yourname! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git@gitee.com
Hi yourname! You've successfully authenticated, but GITEE.COM does not provide shell access.
</code></pre>
<h1 id="5-hexo博客仓库">5. hexo博客仓库</h1>
<pre><code>vi .depoly_git/.git/config 增加
name = username
email = email
</code></pre>
<h1 id="6-针对不同的项目仓库">6. 针对不同的项目仓库</h1>
<p>增加本地配置,在每个仓库的<code>.git/config</code>中进行配置不同的用户,以及其他的配置信息</p>
<pre><code>$ git config --local user.name 'github/gitee账号名'
$ git config --local user.email 'github/gitee账号邮箱'
</code></pre>
<p>--global是在全局配置文件中设置</p>
<p>--local 是针对当前仓库的项目进行设置</p><br><br>
来源:https://www.cnblogs.com/land-fill/p/13510396.html
頁:
[1]