258 lines
5.6 KiB
Markdown
258 lines
5.6 KiB
Markdown
|
|
# 从码云推送到 Gerrit 操作指南
|
|||
|
|
|
|||
|
|
## 📋 前提条件
|
|||
|
|
|
|||
|
|
- ✅ 已从码云(Gitee)拉取最新代码
|
|||
|
|
- ✅ 代码仓库在本地已存在
|
|||
|
|
- ✅ Gerrit 服务器地址:`101.43.95.130:8080`
|
|||
|
|
- ✅ SSH 端口:`29418`
|
|||
|
|
- ✅ 用户名:`renjianbo`
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🚀 操作步骤
|
|||
|
|
|
|||
|
|
### 步骤 1:进入代码仓库目录
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 根据您的实际路径,进入代码目录
|
|||
|
|
cd /d/zhini/zhini_im
|
|||
|
|
# 或者
|
|||
|
|
cd /path/to/your/project
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 2:检查当前 Git 状态
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 查看当前分支和状态
|
|||
|
|
git status
|
|||
|
|
|
|||
|
|
# 查看远程仓库配置
|
|||
|
|
git remote -v
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 3:添加 Gerrit 远程仓库
|
|||
|
|
|
|||
|
|
**如果还没有配置 Gerrit 远程仓库**:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 方式一:添加为新的远程仓库(推荐,保留码云远程)
|
|||
|
|
git remote add gerrit ssh://renjianbo@101.43.95.130:29418/项目名称
|
|||
|
|
|
|||
|
|
# 方式二:替换 origin 为 Gerrit(会替换掉码云的远程)
|
|||
|
|
# git remote set-url origin ssh://renjianbo@101.43.95.130:29418/项目名称
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**注意**:请将 `项目名称` 替换为 Gerrit 中的实际项目名称。
|
|||
|
|
|
|||
|
|
**如果使用 HTTP(不推荐,但可以作为备选)**:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
git remote add gerrit http://renjianbo@101.43.95.130:8080/项目名称
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 4:安装 commit-msg Hook(重要!)
|
|||
|
|
|
|||
|
|
Gerrit 需要 Change-Id,必须安装 hook:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 下载 commit-msg hook
|
|||
|
|
mkdir -p .git/hooks
|
|||
|
|
curl -o .git/hooks/commit-msg http://101.43.95.130:8080/tools/hooks/commit-msg
|
|||
|
|
chmod +x .git/hooks/commit-msg
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 5:检查是否有未提交的更改
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 查看工作区状态
|
|||
|
|
git status
|
|||
|
|
|
|||
|
|
# 如果有未提交的更改,需要先提交
|
|||
|
|
git add .
|
|||
|
|
git commit -m "从码云同步代码"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 6:推送到 Gerrit
|
|||
|
|
|
|||
|
|
**推送到评审队列(推荐)**:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 推送到 master 分支的评审队列
|
|||
|
|
git push gerrit HEAD:refs/for/master
|
|||
|
|
|
|||
|
|
# 如果替换了 origin,使用:
|
|||
|
|
# git push origin HEAD:refs/for/master
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**推送到其他分支**:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 推送到 develop 分支的评审队列
|
|||
|
|
git push gerrit HEAD:refs/for/develop
|
|||
|
|
|
|||
|
|
# 推送到 feature 分支的评审队列
|
|||
|
|
git push gerrit HEAD:refs/for/feature/xxx
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 7:查看推送结果
|
|||
|
|
|
|||
|
|
推送成功后,会显示类似以下信息:
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
remote: Resolving deltas: 100% (X/X)
|
|||
|
|
remote: Processing changes: refs: 1, new: 1
|
|||
|
|
remote:
|
|||
|
|
remote: SUCCESS
|
|||
|
|
remote:
|
|||
|
|
remote: http://101.43.95.130:8080/c/项目名称/+/1 [NEW]
|
|||
|
|
remote:
|
|||
|
|
To ssh://renjianbo@101.43.95.130:29418/项目名称
|
|||
|
|
* [new branch] HEAD -> refs/for/master
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
复制返回的 URL(如:`http://101.43.95.130:8080/c/项目名称/+/1`)在浏览器中打开查看变更。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🔧 常见问题处理
|
|||
|
|
|
|||
|
|
### 问题 1:找不到项目
|
|||
|
|
|
|||
|
|
**错误信息**:
|
|||
|
|
```
|
|||
|
|
fatal: Could not read from remote repository.
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**解决方法**:
|
|||
|
|
1. 确认项目名称是否正确
|
|||
|
|
2. 在 Gerrit Web 界面查看项目列表:`http://101.43.95.130:8080/#/admin/projects/`
|
|||
|
|
3. 如果项目不存在,需要先在 Gerrit 中创建项目
|
|||
|
|
|
|||
|
|
### 问题 2:SSH 连接失败
|
|||
|
|
|
|||
|
|
**错误信息**:
|
|||
|
|
```
|
|||
|
|
ssh: connect to host 101.43.95.130 port 29418: Connection timed out
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**解决方法**:
|
|||
|
|
1. 检查 SSH 公钥是否已添加到 Gerrit:`http://101.43.95.130:8080/#/settings/ssh-keys`
|
|||
|
|
2. 测试 SSH 连接:
|
|||
|
|
```bash
|
|||
|
|
ssh -p 29418 renjianbo@101.43.95.130 gerrit version
|
|||
|
|
```
|
|||
|
|
3. 如果 SSH 无法使用,可以临时使用 HTTP
|
|||
|
|
|
|||
|
|
### 问题 3:缺少 Change-Id
|
|||
|
|
|
|||
|
|
**错误信息**:
|
|||
|
|
```
|
|||
|
|
remote: ERROR: missing Change-Id in commit message
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**解决方法**:
|
|||
|
|
1. 确保已安装 commit-msg hook(见步骤 4)
|
|||
|
|
2. 如果已安装但仍有问题,重新安装:
|
|||
|
|
```bash
|
|||
|
|
curl -o .git/hooks/commit-msg http://101.43.95.130:8080/tools/hooks/commit-msg
|
|||
|
|
chmod +x .git/hooks/commit-msg
|
|||
|
|
```
|
|||
|
|
3. 对于已提交的 commit,使用 `git commit --amend` 重新提交
|
|||
|
|
|
|||
|
|
### 问题 4:权限不足
|
|||
|
|
|
|||
|
|
**错误信息**:
|
|||
|
|
```
|
|||
|
|
remote: Permission denied
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**解决方法**:
|
|||
|
|
1. 确认您在 Gerrit 中已注册账户
|
|||
|
|
2. 确认您有该项目的推送权限
|
|||
|
|
3. 联系管理员添加权限
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 📝 完整示例
|
|||
|
|
|
|||
|
|
假设您的项目名称是 `zhini_im`,完整操作如下:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 1. 进入项目目录
|
|||
|
|
cd /d/zhini/zhini_im
|
|||
|
|
|
|||
|
|
# 2. 检查状态
|
|||
|
|
git status
|
|||
|
|
git remote -v
|
|||
|
|
|
|||
|
|
# 3. 添加 Gerrit 远程仓库
|
|||
|
|
git remote add gerrit ssh://renjianbo@101.43.95.130:29418/zhini_im
|
|||
|
|
|
|||
|
|
# 4. 安装 commit-msg hook
|
|||
|
|
mkdir -p .git/hooks
|
|||
|
|
curl -o .git/hooks/commit-msg http://101.43.95.130:8080/tools/hooks/commit-msg
|
|||
|
|
chmod +x .git/hooks/commit-msg
|
|||
|
|
|
|||
|
|
# 5. 如果有未提交的更改,先提交
|
|||
|
|
git add .
|
|||
|
|
git commit -m "从码云同步代码到 Gerrit"
|
|||
|
|
|
|||
|
|
# 6. 推送到 Gerrit
|
|||
|
|
git push gerrit HEAD:refs/for/master
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🎯 快速参考
|
|||
|
|
|
|||
|
|
### 常用命令
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 查看远程仓库
|
|||
|
|
git remote -v
|
|||
|
|
|
|||
|
|
# 添加 Gerrit 远程仓库
|
|||
|
|
git remote add gerrit ssh://renjianbo@101.43.95.130:29418/项目名称
|
|||
|
|
|
|||
|
|
# 推送到评审队列
|
|||
|
|
git push gerrit HEAD:refs/for/master
|
|||
|
|
|
|||
|
|
# 查看变更列表
|
|||
|
|
# 浏览器访问:http://101.43.95.130:8080/#/q/status:open
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 查看项目列表
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 通过 SSH
|
|||
|
|
ssh -p 29418 renjianbo@101.43.95.130 gerrit ls-projects
|
|||
|
|
|
|||
|
|
# 通过 Web 界面
|
|||
|
|
# 访问:http://101.43.95.130:8080/#/admin/projects/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## ✅ 检查清单
|
|||
|
|
|
|||
|
|
在推送前,请确认:
|
|||
|
|
|
|||
|
|
- [ ] 已进入正确的代码仓库目录
|
|||
|
|
- [ ] 已添加 Gerrit 远程仓库
|
|||
|
|
- [ ] 已安装 commit-msg hook
|
|||
|
|
- [ ] 已提交所有更改(或确认没有未提交的更改)
|
|||
|
|
- [ ] 知道目标分支名称(master/develop 等)
|
|||
|
|
- [ ] SSH 公钥已添加到 Gerrit(如果使用 SSH)
|
|||
|
|
- [ ] 项目已在 Gerrit 中创建
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 📞 需要帮助?
|
|||
|
|
|
|||
|
|
- 📖 查看其他文档:`Gerrit快速使用指南.md`
|
|||
|
|
- 🌐 访问 Gerrit:`http://101.43.95.130:8080`
|
|||
|
|
- 📝 查看变更:`http://101.43.95.130:8080/#/q/status:open`
|
|||
|
|
|
|||
|
|
|