作者:机智的爆爆哥
环境:macOS + GitLab Self-Managed(多 IP 内网部署)
工具:
glab(GitLab 官方 CLI)
在企业开发中,我们常常会遇到 多个 GitLab 实例 的场景:测试环境、预发环境、生产环境分别部署在不同 IP 上。此时,如何用命令行高效管理这些仓库?git 命令本身不支持 API 操作(如创建 Merge Request),而 glab(GitLab CLI)正是官方推出的解决方案。
本文将手把手教你:
- 安装
glab - 配置多 GitLab 实例(含非标准端口)
- 自动生成
git mrc别名,一键创建 MR - 避开常见坑点(如
GITLAB_HOST环境变量干扰)
🔒 安全提示:文中所有 IP 和 Token 均已脱敏,请替换为你自己的实际值。
一、安装 glab
macOS(推荐使用 Homebrew):
bash
brew install glab
验证安装:
bash
glab --version
# 输出示例:glab version 1.32.0 (2024-12-09)
二、配置多 GitLab 实例(关键!)
glab 的配置文件默认位于:
bash
~/Library/Application Support/glab-cli/config.yml
⚠️ 重要 :不要 在 Shell 环境中设置
GITLAB_HOST!该变量会强制覆盖自动识别逻辑,导致"remote 不匹配"错误。
✅ 正确配置示例(支持 3 个内网 GitLab):
yaml
git_protocol: http
editor:
browser:
glamour_style: dark
check_update: false
display_hyperlinks: false
no_prompt: false
telemetry: true
# 关键:不要设置全局 host!让 glab 自动从 remote 匹配
# host: GITLAB_IP_1 # ← 注释掉这行!
hosts:
# === GitLab 1: 标准 HTTP 80 端口 ===
GITLAB_IP_1:
token: YOUR_TOKEN_1
api_protocol: http
api_host: GITLAB_IP_1
git_protocol: http
container_registry_domains: []
# === GitLab 2: 非标准端口(如 8099)===
GITLAB_IP_2:
token: YOUR_TOKEN_2
api_protocol: http
api_host: GITLAB_IP_2:8099 # ← 端口写在这里
git_protocol: http
container_registry_domains: []
# === GitLab 3: 另一个内网实例 ===
GITLAB_IP_3:
token: YOUR_TOKEN_3
api_protocol: http
api_host: GITLAB_IP_3
git_protocol: http
container_registry_domains: []
🔑 配置要点:
| 项目 | 说明 |
|---|---|
hosts 键名 |
必须是 纯 IP (如 192.168.x.x),不能带端口或 / |
api_host |
可带端口(如 172.16.x.x:8099) |
token |
在对应 GitLab 实例的 Personal Access Tokens 页面创建,需勾选 api + write_repository |
git_protocol |
必须与你 git clone 时用的协议一致(本文均为 http) |
全局 host 字段 |
建议注释掉 ,让 glab 自动从 remote URL 识别 |
三、创建 Personal Access Token
在每个 GitLab 实例上操作:
- 登录 → 点击头像 → Edit profile
- 左侧菜单 → Personal Access Tokens
- 创建新 Token:
- Name:
glab-cli - Scopes: ✅
api+ ✅write_repository - Expiry: 建议 365 天
- Name:
- 复制生成的 Token(只显示一次!)
📌 Token 权限说明:
api:允许调用 GitLab API(包括创建 MR)write_repository:允许通过 HTTP 推送代码
四、设置 git mrc 别名(一键创建 MR)
在终端执行:
bash
git config --global alias.mrc '!f() { \
glab mr create \
--source-branch "$1" \
--target-branch "$2" \
--title "Git合并: $1 -> $2" \
--description "命令行创建的合并请求" \
--yes; \
}; f'
使用方式:
bash
# 创建从 feature-xxx 到 dev 的合并请求
git mrc feature-xxx dev
# 创建从 xx 到 test 的合并请求
git mrc xx test
✅
--yes自动跳过交互确认,适合脚本化。
五、常见问题排查
❌ 报错:
None of the git remotes configured for this repository correspond to the GITLAB_HOST environment variable.
✅ 解决方案:
-
确认未设置
GITLAB_HOST:bashecho $GITLAB_HOST # 应无输出 unset GITLAB_HOST # 临时清除 -
确认 remote URL 与配置匹配 :
bashgit remote -v # 输出应为:http://GITLAB_IP_3/group/project.git -
确认
hosts键名是纯 IP(无端口)
💡 终极绕过命令(推荐):
bashGITLAB_HOST= git mrc source target
六、验证配置是否生效
bash
# 查看当前项目关联的 GitLab 配置
glab config view
# 测试 API(以 GITLAB_IP_3 为例)
glab api --host GITLAB_IP_3 /user
如果返回用户信息,说明配置成功!
七、总结
| 步骤 | 关键点 |
|---|---|
| 安装 | brew install glab |
| 配置 | config.yml 中 hosts 键名 = 纯 IP |
| Token | 每个 GitLab 实例独立申请,权限 api + write_repository |
| 别名 | git mrc source target 一键创建 MR |
| 避坑 | 不要设置 GITLAB_HOST ,不要带端口写 host 键名 |
通过上述配置,你可以在 同一个终端 中无缝切换多个 GitLab 项目,彻底告别网页点点点!
💡 推荐 :将
config.yml加入你的 dotfiles 管理,方便在新机器快速部署。🔒 再次强调:切勿将真实 Token 或 IP 提交到公开仓库!
如果你觉得这篇教程有用,欢迎点赞、收藏、转发!
有任何问题,欢迎在评论区交流 👇