想象你正在与多个组织合作,每个组织代码版本管理方式各异。例如,你可能需要使用工作邮箱创建一个新的 Git 账户,而你已经为个人账号设置了 Git 环境。在另一种情况下,可能每个组织使用不同的版本控制平台,如 GitHub、GitLab 或 Bitbucket 等。
这个快速入门将带你一步步管理这种配置。
使用 SSH:配置多个 Git 账号我们将涵盖两种你可能遇到的情况:
- 单一的版本控制平台(例如 GitHub 这样的平台)支持多种认证方法。
- 不同的版本控制平台,各自有自己的认证方法。
以下是我们将要实施的SSH认证流程的图形展示。
生成独特的 SSH 密钥对
正确地命名你的密钥非常重要。我建议使用这种命名方式:nameofvcplatform_nameoforganization_rsa
,这就是一个例子,例如,github_tesla_rsa
。
# GitHub 个人账号
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/github_personal_rsa
# GitHub 组织账号
ssh-keygen -t rsa -b 4096 -C "你的电子邮件@organization-name.com" -f ~/.ssh/github_组织名_rsa
# GitLab
ssh-keygen -t rsa -b 4096 -C "你的电子邮件@organization-name.com" -f ~/.ssh/gitlab_组织名_rsa
# 比特巴克
ssh-keygen -t rsa -b 4096 -C "你的电子邮件@organization-name.com" -f ~/.ssh/bitbucket_组织名_rsa
点击此处进入全屏,点击此处退出全屏
步骤 2:添加 SSH 密钥到 SSH 代理
在我们这里,将是:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/github_personal_rsa
ssh-add ~/.ssh/github_organizationname_rsa
ssh-add ~/.ssh/gitlab_organizationname_rsa
ssh-add ~/.ssh/bitbucket_organizationname_rsa
您可以进入全屏模式。您可以退出全屏模式。
第三步:在每个平台添加 SSH 密钥
复制公共密钥并将其添加到你的 Git 账户中。在 Linux 上,可以使用 cat
命令来显示公钥。或者,根据你的操作系统找到相应的命令,或者直接打开密钥文件,复制其中的内容文本。
# 复制你的公钥
# 这里的命令用于显示公钥内容
cat ~/.ssh/github_personal_rsa
cat ~/.ssh/github_organizationname_rsa
cat ~/.ssh/gitlab_organizationname_rsa
cat ~/.ssh/bitbucket_organizationname_rsa
进入全屏,退出全屏
GitHub: 去 [SSH 和 GPG 密钥 https://github.com/settings/keys]。
GitLab: 进入 SSH 密钥。
Bitbucket: 进入 SSH 密钥。
第 4 步. 配置 SSH 配置
创建或修改 ~/.ssh/配置文件
,并指定 SSH 如何处理各个账号。
SSH配置文件非常重要,因为它定义了主机,让你可以通过名称来区分不同的版本控制平台和账户。看看我们仓库的SSH配置。
注意如何配置 Host
和 IdentityFile
。
# 个人GitHub账号
Host github-personal.com
HostName github.com
User git
IdentityFile ~/.ssh/github_personal_rsa
# 公司或团队的GitHub账号
Host github-organizationname.com
HostName github.com
User git
IdentityFile ~/.ssh/github_organizationname_rsa
# 公司或团队的GitLab账号
Host gitlab-organizationname.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/gitlab_organizationname_rsa
# 公司或团队的Bitbucket账号
Host bitbucket-organizationname.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/bitbucket_organizationname_rsa
点击全屏 点击退出
第五步:使用这些代码仓库
克隆、拉取或推送的命令会随你在 SSH 配置中定义的自定义主机名变化。
比如说:
1. 克隆代码仓库,
你将使用在SSH配置中定义的自定义主机名,而不是默认的那些(例如github.com, gitlab.com)。
GitHub 账号:
克隆代码库到本地:git clone [email protected]:username/repo.git
点击进入全屏 点击退出全屏
GitHub 上的组织:
git clone [email protected]:organization/repo.git
这是从GitHub组织仓库克隆代码的命令。
切换到全屏 | 退出全屏
**GitLab:**
git clone [email protected]:organization/repo.git
克隆代码仓库的命令。此命令用于从 GitLab 拉取代码库,以便在本地环境中使用。
全屏进入 全屏退出
Bitbucket:
git clone [email protected]:organization/repo.git (从Bitbucket克隆仓库到本地)
2. 拉取或推送更改内容
当你拉取或推送更改时,你将使用相同的格式,并使用自定义的主机名。
GitHub 个人页面:
在终端中输入以下命令:
git pull [email protected]:username/repo.git # 拉取最新代码
git push [email protected]:username/repo.git # 推送本地更改
进入全屏,退出全屏
GitHub组织页面:
git pull [email protected]:organization/repo.git
git push [email protected]:organization/repo.git
上述命令用于从GitHub拉取最新代码并推送更改。你可以使用这些命令来同步你的本地代码库与远程仓库。
点击全屏 退出全屏
GitLab:
git pull [email protected]:organization/repo.git
git push [email protected]:organization/repo.git
进入全屏,退出全屏
Bitbucket:
请从仓库拉取最新代码:git pull [email protected]:organization/repo.git
请推送你的更改到仓库:git push [email protected]:organization/repo.git
全屏显示,退出全屏 (点击全屏显示或退出全屏)
**3. 为现有仓库设置远程 URL 地址。**
如果你想更新现有仓库的远程 URL(即远程地址):可以试试下面的命令:
_**GitHub 个人页面:**_
将远程仓库的URL设置为个人GitHub上的地址:[email protected]:username/repo.git
切换到全屏并随时退出
_**GitHub 组织:**_
git remote set-url origin [email protected]:organization/repo.git
将远程仓库的URL设置为GitHub上的组织仓库
进入全屏,退出全屏
_**GitLab:**_
git remote set-url origin [email protected]:organization/repo.git # 将远程仓库的 URL 设置为 GitLab 上的组织仓库地址
全屏 全屏退出
_**Bitbucket:(代码托管平台)**_
git remote set-url origin [email protected]:organization/repo.git
进入全屏 退出全屏
这些设置确保 Git 使用你在 SSH 配置文件中为指定的主机设定的 SSH 密钥。
希望这篇文章对你有帮助,谢谢!
共同學習,寫下你的評論
評論加載中...
作者其他優質文章