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

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

1. 生成 SSH 密钥

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

bash 复制代码
ssh-keygen -t rsa -C "[email protected]"

这里的 "[email protected]" 应该替换为你在 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 [email protected]
ssh -T [email protected]

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

5. 配置 Git 用户名和邮箱

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

bash 复制代码
git config user.name "Your Name"
git config user.email "[email protected]"

这里的 "Your Name" 和 "[email protected]" 应该替换为你在 GitLab 和 Gitee 上注册的用户名和邮箱。

6. 配置远程仓库地址

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

bash 复制代码
git remote add origin_gitlab [email protected]:username/repo.git
git remote add origin_gitee [email protected]:username/repo.git

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

相关推荐
Baoing_5 小时前
Git 项目切换到新的远程仓库地址
git
暴躁哥10 小时前
Git 版本控制系统入门指南
git
diving deep12 小时前
IDEA中git对于指定文件进行版本控制
git
趁你还年轻_15 小时前
记录一次git提交失败解决方案
git
关于不上作者榜就原神启动那件事18 小时前
git版本控制学习
git·学习
Cchaofan1 天前
Git/GitLab日常使用的命令指南来了!
git·gitlab
可乐加.糖1 天前
项目版本管理和Git分支管理方案
java·git·目标跟踪·gitlab·敏捷流程·源代码管理
wingaso1 天前
[经验总结]删除gitlab仓库分支报错:错误:无法推送一些引用到“http:”
linux·数据仓库·git
ice___Cpu2 天前
Git - 1( 14000 字详解 )
大数据·git·elasticsearch
GoodStudyAndDayDayUp2 天前
gitlab+portainer 实现Ruoyi Vue后端CI/CD
vue.js·ci/cd·gitlab