【Tools】Repo 工具完整使用手册

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
相关推荐
tianyuanwo2 小时前
跨 Gerrit 项目迁移分支并保留完整历史:一份可操作的 Git 指南
git·代码迁移
玄奕子2 小时前
VS Code 上传 GitHub 全流程(Windows 环境):HTTP 与 SSH 两种方案(含常见报错排查)
git·http·ssh·github·嵌入式开发
一只游鱼2 小时前
如何让本地的敏感配置文件不上传到git仓库
git·elasticsearch
渣渣馬15 小时前
shell的if多条件
git·ssh
zh_xuan15 小时前
Visual Studio 上传工程到github
ide·git·github·visual studio
AntoineGriezmann17 小时前
Git 学习笔记
git
无限进步_17 小时前
【C++】只出现一次的数字 II:位运算的三种解法深度解析
数据结构·c++·ide·windows·git·算法·leetcode
无限进步_19 小时前
【C++】多重继承中的虚表布局分析:D类对象为何有两个虚表?
开发语言·c++·ide·windows·git·算法·visual studio
回家路上绕了弯20 小时前
Git worktree 终极指南:告别分支切换烦恼,实现多分支并行开发
git·后端