# 将本地项目上传到 Gerrit 指南 ## 前提条件 1. ✅ Gerrit 已安装并运行:`http://101.43.95.130:8080` 2. ✅ 已在 Gerrit 创建项目:`zhini_im` 3. ✅ 本地已安装 Git ## 方法一:已有 Git 仓库(推荐) 如果 `D:\zhini\zhini_im` 已经是 Git 仓库: ### 步骤 1:打开 Git Bash 或命令行 在 Windows 上: - 打开 Git Bash(如果安装了 Git for Windows) - 或者打开 PowerShell/CMD ### 步骤 2:进入项目目录 ```bash cd /d/zhini/zhini_im # 或 cd D:\zhini\zhini_im ``` ### 步骤 3:添加 Gerrit 远程仓库 ```bash # 添加 Gerrit 作为远程仓库 git remote add gerrit http://101.43.95.130:8080/zhini_im # 或使用 SSH(如果配置了 SSH 密钥) git remote add gerrit ssh://admin@101.43.95.130:29418/zhini_im ``` ### 步骤 4:推送代码到 Gerrit ```bash # 推送主分支 git push gerrit HEAD:refs/heads/master # 或者推送所有分支 git push gerrit --all ``` ## 方法二:首次上传(创建初始提交) 如果项目还没有 Git 仓库: ### 步骤 1:初始化 Git 仓库 ```bash cd D:\zhini\zhini_im git init ``` ### 步骤 2:添加所有文件 ```bash git add . ``` ### 步骤 3:创建初始提交 ```bash git commit -m "Initial commit" ``` ### 步骤 4:配置 Git 用户信息(如果还没配置) ```bash git config user.name "admin" git config user.email "admin@example.com" ``` ### 步骤 5:添加 Gerrit 远程仓库 ```bash git remote add gerrit http://101.43.95.130:8080/zhini_im ``` ### 步骤 6:推送代码 ```bash git push gerrit HEAD:refs/heads/master ``` ## 方法三:通过代码评审推送(推荐用于后续更新) ### 步骤 1:配置 Git 远程仓库 ```bash cd D:\zhini\zhini_im git remote add gerrit http://101.43.95.130:8080/zhini_im ``` ### 步骤 2:推送代码进行评审 ```bash # 推送当前分支到评审队列 git push gerrit HEAD:refs/for/master # 如果需要指定主题 git push gerrit HEAD:refs/for/master%topic=feature-name ``` ### 步骤 3:在 Gerrit Web 界面进行评审 1. 访问:`http://101.43.95.130:8080` 2. 在 "Outgoing reviews" 中查看您的变更 3. 等待评审或自己评审后合并 ## 详细操作步骤(Windows) ### 1. 打开 Git Bash - 在项目目录 `D:\zhini\zhini_im` 右键 - 选择 "Git Bash Here" ### 2. 检查是否已有 Git 仓库 ```bash # 检查是否有 .git 目录 ls -la | grep .git ``` ### 3. 如果没有 Git 仓库,初始化 ```bash git init git add . git commit -m "Initial commit" ``` ### 4. 配置 Git 用户信息 ```bash git config user.name "admin" git config user.email "admin@example.com" # 或全局配置 git config --global user.name "admin" git config --global user.email "admin@example.com" ``` ### 5. 添加 Gerrit 远程仓库 ```bash # 使用 HTTP(推荐,简单) git remote add gerrit http://101.43.95.130:8080/zhini_im # 或使用 SSH(需要配置 SSH 密钥) git remote add gerrit ssh://admin@101.43.95.130:29418/zhini_im ``` ### 6. 推送代码 ```bash # 直接推送到主分支(首次上传) git push gerrit HEAD:refs/heads/master # 或推送到评审队列(后续更新,推荐) git push gerrit HEAD:refs/for/master ``` ## 常见问题 ### Q: 推送时提示需要认证? **A:** 在开发模式下,HTTP 推送可能不需要密码,但如果需要: - 用户名:您注册时使用的用户名(例如:`admin`) - 密码:留空(开发模式不需要密码) ### Q: 推送失败,提示 "remote rejected"? **A:** 可能的原因: 1. 项目不存在 → 先在 Gerrit 创建项目 2. 没有推送权限 → 检查项目权限配置 3. 分支保护 → 使用 `refs/for/master` 推送到评审队列 ### Q: 如何查看远程仓库? ```bash git remote -v ``` ### Q: 如何更新远程仓库地址? ```bash # 删除旧的 git remote remove gerrit # 添加新的 git remote add gerrit http://101.43.95.130:8080/zhini_im ``` ## 推送方式对比 ### 直接推送(`refs/heads/master`) - ✅ 直接更新主分支 - ❌ 跳过代码评审流程 - 适用于:首次上传、管理员直接合并 ### 评审推送(`refs/for/master`) - ✅ 进入代码评审流程 - ✅ 需要评审后才能合并 - ✅ 符合 Gerrit 工作流程 - 推荐用于:日常开发、团队协作 ## 完整示例 ```bash # 1. 进入项目目录 cd D:\zhini\zhini_im # 2. 初始化 Git(如果还没有) git init git add . git commit -m "Initial commit" # 3. 配置用户信息 git config user.name "admin" git config user.email "admin@example.com" # 4. 添加 Gerrit 远程仓库 git remote add gerrit http://101.43.95.130:8080/zhini_im # 5. 推送代码(首次上传) git push gerrit HEAD:refs/heads/master # 6. 后续更新(推送到评审队列) git add . git commit -m "Update code" git push gerrit HEAD:refs/for/master ``` ## 验证上传 推送成功后: 1. 访问:`http://101.43.95.130:8080/#/admin/projects/zhini_im` 2. 应该能看到项目信息 3. 访问:`http://101.43.95.130:8080/#/q/project:zhini_im` 4. 应该能看到您的变更 ## 后续操作 ### 克隆项目(其他团队成员) ```bash # 使用 HTTP git clone http://101.43.95.130:8080/zhini_im # 或使用 SSH git clone ssh://admin@101.43.95.130:29418/zhini_im ``` ### 日常开发流程 ```bash # 1. 修改代码 # 2. 提交更改 git add . git commit -m "描述您的更改" # 3. 推送到评审队列 git push gerrit HEAD:refs/for/master # 4. 在 Gerrit Web 界面进行评审 # 5. 评审通过后,代码会自动合并 ```