Repo 工具完整使用手册
(嵌入式 / Android / OpenHarmony 多仓库项目通用版)
一、Repo 是什么
Repo 是 Google 推出的多 Git 仓库管理工具,用于统一管理几十个甚至上百个 Git 子仓库。
- 通过一个
manifest.xml统一描述所有仓库地址、分支、路径 - 用一套命令批量操作所有 Git 仓库
- 常用于:Android、鸿蒙、芯片 BSP、Linux 大项目
二、基础安装(极简版)
Bash
# 下载 repo
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
# 配置环境变量
export PATH=~/bin:$PATH
三、初始化仓库(最关键一步)
Bash
repo init -u <manifest仓库地址> -b <分支> -m <manifest文件>
示例:
Bash
repo init -u git@github.com:xxx/manifest.git -b master -m default.xml
作用:
- 下载 manifest 仓库
- 生成
.repo/目录 - 记录所有子仓库信息
四、最常用核心命令(必须背熟)
1. 同步代码(日常更新)
Bash
repo sync
2. 最推荐安全同步(企业标准)
Bash
repo sync -c -d
-c:只拉当前分支,更快更小-d:强制切回 manifest 指定的 commit,防止本地错乱
3. 多线程加速(常用 -j8)
Bash
repo sync -c -d -j8
4. 同步后拉 Git LFS 大文件
Bash
repo forall -c git lfs pull
五、批量操作命令(超级实用)
1. 在所有仓库执行任意命令
Bash
repo forall -c "任意shell命令"
示例:
Bash
repo forall -c git status
repo forall -c git branch
repo forall -c git clean -df
2. 显示每个仓库执行标题(方便看日志)
Bash
repo forall -p -c git status
3. 只对某个仓库执行命令
Bash
repo forall kernel-5.10 -c git checkout dev
六、查看信息类命令
1. 查看所有子仓库列表
Bash
repo list
2. 查看每个仓库当前分支/状态
Bash
repo info
3. 查看 manifest 路径
Bash
repo manifest
七、分支操作(开发必备)
1. 给所有仓库新建并切换分支
Bash
repo start 分支名 --all
2. 给指定仓库新建分支
Bash
repo start dev kernel-5.10
3. 查看所有仓库当前分支
Bash
repo branches
八、上传代码(repo upload)
适用于使用 Gerrit 做代码 review 的项目:
Bash
repo upload
会批量把所有有改动的仓库推送到 Gerrit。
九、清理与重置
1. 丢弃所有本地修改(危险!)
Bash
repo forall -c git reset --hard HEAD
2. 清理未跟踪文件
Bash
repo forall -c git clean -df
3. 完全重置整个工程(错乱时救命)
Bash
repo sync -c -d --force-sync
十、完整标准工作流(企业通用)
首次拉代码
Bash
repo init -u git@xxx/manifest.git -b master
repo sync -c -d -j8
repo forall -c git lfs pull
日常更新
Bash
repo sync -c -d
repo forall -c git lfs pull
开发修改
Bash
repo start dev --all
# 修改代码
repo forall -c git status
repo forall -c git add .
repo forall -c git commit -m "update"
十一、常用参数速查表
| 参数 | 全称 | 作用 |
|---|---|---|
-c |
--current-branch | 只拉当前分支,更快 |
-d |
--detach | 强制对齐 manifest |
-jN |
--jobs=N | 多线程下载 |
-f |
--force | 出错继续同步 |
--force-sync |
强制覆盖同步 | |
-p |
执行命令时打印项目名 |
十二、常见问题 FAQ
1. repo sync 会覆盖我本地修改吗?
默认不会。
加 -d 也只会切 HEAD,不会删未提交修改。
真正危险的是 git reset --hard。
2. repo sync 和 git pull 区别?
git pull:单个仓库更新repo sync:批量所有仓库 + 对齐 manifest
3. 为什么要 git lfs pull?
repo sync 不拉大文件,只拉指针,必须手动拉 LFS。
4. 仓库错乱怎么办?
Bash
repo sync -c -d --force-sync
repo forall -c git reset --hard HEAD