设置Mac上Git的多账户配置,用于同时访问GitLab和Gitee

在 Mac 上配置 Git 多账户(比如 GitLab 和 Gitee)的步骤如下:

1. 生成 SSH 密钥

首先,你需要为每个 Git 服务生成一个 SSH 密钥。在终端中运行以下命令,然后按照提示操作:

bash 复制代码
ssh-keygen -t rsa -C "your-email@example.com"

这里的 "your-email@example.com" 应该替换为你在 GitLab 和 Gitee 上注册的邮箱。

2. 添加 SSH 密钥到 SSH 代理

运行以下命令,将 SSH 密钥添加到 SSH 代理中:

bash 复制代码
ssh-add ~/.ssh/id_rsa_gitlab
ssh-add ~/.ssh/id_rsa_gitee

这里的 id_rsa_gitlabid_rsa_gitee 应该替换为你生成的 SSH 密钥的文件名。

3. 配置 Git

~/.ssh 目录下创建一个 config 文件,然后添加以下内容:

bash 复制代码
# GitLab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab

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

这里的 id_rsa_gitlabid_rsa_gitee 应该替换为你生成的 SSH 密钥的文件名。

4. 测试 SSH 连接

运行以下命令,测试 SSH 连接是否正常:

bash 复制代码
ssh -T git@gitlab.com
ssh -T git@gitee.com

如果一切正常,你应该能够看到类似 "Welcome to GitLab, @your-username!" 和 "Welcome to Gitee, @your-username!" 的消息。

5. 配置 Git 用户名和邮箱

在每个 Git 仓库中,你可以设置不同的用户名和邮箱。在仓库目录下,运行以下命令:

bash 复制代码
git config user.name "Your Name"
git config user.email "your-email@example.com"

这里的 "Your Name" 和 "your-email@example.com" 应该替换为你在 GitLab 和 Gitee 上注册的用户名和邮箱。

6. 配置远程仓库地址

在每个项目中,设置远程仓库地址,分别指向 GitLab 和 Gitee 的仓库地址:

bash 复制代码
git remote add origin_gitlab git@gitlab.com:username/repo.git
git remote add origin_gitee git@gitee.com:username/repo.git

通过这些步骤,您可以在 Mac 上成功配置多个 Git 账户,以便同时访问 GitLab 和 Gitee 上的项目。

相关推荐
用户1259265423205 分钟前
使用 Docker 搭建 Gitea 并实现 Git HTTP 自动登录
git
该用户已不存在7 分钟前
关于我把Mac Mini托管到机房,后续来了,还有更多玩法
服务器·前端·mac
一只毛驴3 小时前
谈谈对git stash的理解?
git
荔枝吻3 小时前
【保姆级喂饭教程】GitLab创建用户规范,分支开发规范,提交日志规范
gitlab·git规范·分支规范
云畅新视界5 小时前
从 CODING 停服到极狐 GitLab “接棒”,软件研发工具市场风云再起
人工智能·gitlab
长风破浪会有时呀7 小时前
Git 学习笔记
笔记·git·学习
政安晨8 小时前
政安晨【开源人工智能硬件】【ESP乐鑫篇】 —— 在macOS上部署工具开发环境(小资的非开发者用苹果系统也可以玩乐鑫)
esp32·mac·开源硬件·环境部署·乐鑫·开源人工智能硬件·工具链搭建
hwj运维之路8 小时前
GitOps实践指南:GitLab CI/CD + ArgoCD 实现 Kubernetes 自动化部署
ci/cd·gitlab·argocd
中微子15 小时前
Git Rebase 详解:概念、原理与实战示例
git
荔枝吻18 小时前
【保姆级喂饭教程】Windows下安装Git Flow
windows·git·git flow