关于git:Git不同仓库使用不同的Key

3次阅读

共计 751 个字符,预计需要花费 2 分钟才能阅读完成。

生成 Key

ssh-keygen -t rsa
# 我把 key 生成在 ~/.ssh/github 目录下,文件名为 leonard-repo,文件如下:
# ~/.ssh/github/leonard-repo
# ~/.ssh/github/leonard-repo.pub
# 查看公钥: cat ~/.ssh/github/leonard-repo.pub

在 GitHub 中退出 Key

网页 github.com -> 某个仓库 [假如为 leonard-repo] -> Setting -> Deploy keys

新建一个 key,把 leonard-repo.pub 这个公钥的内容复制到其中

本地设置

SSH 设置

编辑 ~/.ssh/config,退出以下内容

# leonard-repo 当初是 github.com 的别名
# 设置别名是避免烦扰其余仓库
# 名字轻易起,然而须要和待会的 Git 本地仓库设置 中的统一

# github.com/leonard/leonard-repo
Host leonard-repo
Hostname github.com
identityFile ~/.ssh/github/leonard-repo
User git

Git 本地仓库设置

编辑 我的项目 /.git/config

[remote "origin"]
    url = git@github.com:leonard/leonard-repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

中的 github.com 改为 leonard-repo,也就是上面这样

[remote "origin"]
    url = git@leonard-repo:leonard/leonard-repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

END

正文完
 0