Files
gerrit/解决推送被拒绝问题.md
2025-12-22 17:12:39 +08:00

186 lines
3.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 解决推送被拒绝问题
## 问题分析
1. **远程仓库已有内容**Gerrit 创建项目时可能已创建初始提交
2. **未提交的更改**:子模块 `android-chat-master` 有修改
3. **推送被拒绝**:需要先拉取远程内容
## 解决方案
### 方法一:合并远程内容(推荐)
```bash
# 1. 处理未提交的更改
# 选项 A提交子模块更改
git add android-chat-master
git commit -m "Update submodule"
# 或选项 B暂存子模块更改
git stash
# 2. 拉取远程内容并允许不相关历史
git pull gerrit master --allow-unrelated-histories
# 3. 如果有冲突,解决冲突后提交
# git add .
# git commit -m "Merge remote changes"
# 4. 推送代码
git push gerrit rjb_dev:refs/heads/master
```
### 方法二:强制推送(如果确定要覆盖远程内容)
⚠️ **注意**:这会覆盖远程仓库的内容,请谨慎使用!
```bash
# 1. 提交所有更改
git add .
git commit -m "Update code"
# 2. 强制推送(覆盖远程)
git push gerrit rjb_dev:refs/heads/master --force
```
### 方法三:推送到新分支(避免冲突)
```bash
# 1. 提交所有更改
git add .
git commit -m "Update code"
# 2. 推送到新分支例如rjb_dev
git push gerrit rjb_dev:refs/heads/rjb_dev
# 然后在 Gerrit 中合并分支
```
## 完整操作步骤(推荐)
### 在 Git Bash 中执行:
```bash
# 1. 确保在项目目录
cd /d/zhini/zhini_im
# 2. 查看当前状态
git status
# 3. 处理子模块更改(选择一种方式)
# 方式 A提交子模块更改
cd android-chat-master
git add .
git commit -m "Update submodule"
cd ..
git add android-chat-master
git commit -m "Update submodule reference"
# 或方式 B暂存子模块更改如果不想提交
# git stash
# 4. 拉取远程内容
git pull gerrit master --allow-unrelated-histories
# 5. 如果有冲突,解决冲突
# 编辑冲突文件,然后:
# git add .
# git commit -m "Merge remote changes"
# 6. 推送代码
git push gerrit rjb_dev:refs/heads/master
```
## 如果拉取时出现冲突
### 解决冲突步骤:
```bash
# 1. 查看冲突文件
git status
# 2. 编辑冲突文件,解决冲突标记
# <<<<<<< HEAD
# 您的代码
# =======
# 远程代码
# >>>>>>> gerrit/master
# 3. 标记冲突已解决
git add .
# 4. 完成合并
git commit -m "Merge remote changes from Gerrit"
# 5. 推送
git push gerrit rjb_dev:refs/heads/master
```
## 推荐方案:推送到评审队列
如果不想处理合并,可以直接推送到评审队列:
```bash
# 1. 提交所有更改
git add .
git commit -m "Update code"
# 2. 推送到评审队列(不会冲突)
git push gerrit rjb_dev:refs/for/master
```
这样代码会进入评审流程,评审通过后会自动合并。
## 快速解决方案
### 最简单的方法(推送到评审队列):
```bash
cd /d/zhini/zhini_im
# 提交所有更改
git add .
git commit -m "Initial push to Gerrit"
# 推送到评审队列(推荐,不会冲突)
git push gerrit rjb_dev:refs/for/master
```
### 如果需要直接推送到主分支:
```bash
cd /d/zhini/zhini_im
# 提交所有更改
git add .
git commit -m "Initial push to Gerrit"
# 拉取远程内容
git pull gerrit master --allow-unrelated-histories
# 解决冲突(如果有)
# 然后推送
git push gerrit rjb_dev:refs/heads/master
```
## 验证推送
推送成功后:
1. **访问变更页面**
```
http://101.43.95.130:8080/#/q/project:zhini_im
```
2. **访问项目页面**
```
http://101.43.95.130:8080/#/admin/projects/zhini_im
```
3. **查看代码**
```
http://101.43.95.130:8080/#/admin/projects/zhini_im,general
```