Git 多密钥配置和多平台托管

1 minute read

一、多密钥配置

1.1 生成密钥

如果是 Windows 系统,建议使用安装 Git 时自带的 git bash 终端。

假设你有三个账号,

  • 一个是 Gitee 的,登录名为 copillar
  • 一个是 Github 的登录名为 wholegospel
  • 一个是 Github 的登录名为 fengxinbao
ssh-keygen -t rsa -C "co@gmail.com" -f ~/.ssh/id_rsa_gitee_co
ssh-keygen -t rsa -C "wg@gmail.com" -f ~/.ssh/id_rsa_github_wg
ssh-keygen -t rsa -C "fxb@gmail.com" -f ~/.ssh/id_rsa_github_fxb

1.2 添加密钥到 ssh-agent 中

Git 默认读取的文件文件名为 id_rsa,因此我们需要将生成的密钥添加到 ssh-agent 中。

添加密钥地址到 ssh-agent:

$ ssh-add ~/.ssh/id_rsa_gitee_co
$ ssh-add ~/.ssh/id_rsa_github_wg
$ ssh-add ~/.ssh/id_rsa_github_fxb

如果出错,可以使用这条命令,再执行上方代码:

$ ssh-agent bash

1.3 编写配置文件

编写 config 文件(~/.ssh目录下),若没有则自行新建。

# github
Host github.com-wg
   HostName github.com
   #PreferredAuthentications publickey  
   IdentityFile ~/.ssh/id_rsa_github_wg

# github
Host github.com-fxb
   HostName github.com
   #PreferredAuthentications publickey  
   IdentityFile ~/.ssh/id_rsa_github_fxb 

# gitee
Host gitee.com-co
   HostName gitee.com
   #PreferredAuthentications publickey  
   IdentityFile ~/.ssh/id_rsa_gitee

# 相关参数
# Host:对识别的模式,进行配置对应的主机名和ssh文件
# HostName : 登录主机的主机名
# PreferredAuthentications :设置登录方式(publickey为使用密钥登录,password为使用密码登录)
# IdentityFile :私钥全路径名

1.4 将公钥添加到 Github 和 Gitee SSH 设置中

1.5 测试是否添加成功

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

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

测试完成后,如果上传代码出现被拒绝,建议先重启电脑。

1.6 建立远程仓库并拉取到本地

登录 Github 的 wholegospel 账号,建立仓库 wholegospel.github.io,并拉取到本地:

$ git clone git@github.com-wg:wholegospel/wholegospel.github.io.git

$ git remote -v
origin	git@github.com-wg:wholegospel/wholegospel.github.io.git (fetch)
origin	git@github.com-wg:wholegospel/wholegospel.github.io.git (push)

注意:仓库 wholegospel.github.io 的拉取地址为 git@github.com:wholegospel/wholegospel.github.io.git,但我们将中间的 github.com 替换成了 github.com-wg,这是为了与本地 ~/.ssh/config 中的配置保持一致,否则会出现无法推送本地更新到远程的情况。

登录 Github 的 fengxinbao 账号,建立仓库 fengxinbao.github.io,并拉取到本地:

$ git@github.com-fxb:fengxinbao/fengxinbao.github.io.git

$ git remote -v
origin	git@github.com-fxb/fengxinbao.github.io.git (fetch)
origin	git@github.com-fxb/fengxinbao.github.io.git (push)

注意:仓库 fengxinbao.github.io 的拉取地址为 git@github.com:fengxinbao/fengxinbao.github.io.git,但我们将中间的 github.com 替换成了 github.com-fxb,这是为了与本地 ~/.ssh/config 中的配置保持一致,否则会出现无法推送本地更新到远程的情况。

Gitee 仓库的建立和拉取过程省略不写。

二、多平台托管

2.1 一个项目同时托管在 Gitee 和 Github 上

这里选用一个已经托管到 Gitee上项目进行演示。

将其关联到 Github 仓库(已创建)上:

# 添加名为github的远程仓库
git remote add github git@github.com:lime2019/purble-pairs.git
# 查看当前远程信息,默认远程名称为origin
git remote -v
# 删除一个远程
git remote rm origin

2.2 项目推送

项目代码修改完成后,推送到远端,则:

# 向github推送
git push github main
# 向Gitee推送
git push gitee main

2.3 使用脚本

虽然按照上面操作,我们实现了,一个项目托管在两个平台上,但是每一次推送都得输两次推送命令,有点麻烦。所以,可以编写脚本实现一条命令同时向两个托管平台推送代码。

在项目目录下新建 deploy.sh

#!/usr/bin/env sh

# 确保脚本抛出遇到的错误
set -e

# git操作
git add .
git commit -m 'deploy'

git push github main

git push gitee main

然后在终端中直接运行 ./deploy.sh 即可。

Armando Maynez

Armando Maynez

Engineer, industry executive, research enthusiast. Avid learner with diverse interests in coding, machine learning, artificial intelligence and reinforcement learning. 17+ years of experience working in multinational corporations.

Comments

  Write a comment ...