git-bundle
将远端 git 仓库打包为 bundle 文件,用于归档。
使用方式
/git-bundle https://github.com/user/repo.git
/git-bundle https://github.com/user/repo.git my-archive
参数
$1— git-repo-url(必填):远端 git 仓库的克隆 URL(支持 https 和 ssh 协议)$2— bundle-name(可选):生成的 bundle 文件名(不含后缀)。若不提供,默认使用仓库名
步骤
-
解析参数:从
$ARGUMENTS中提取 URL 和可选的 bundle 名称。
- 如果提供了 bundle 名称,则输出文件为<bundle-name>.bundle
- 如果未提供 bundle 名称,则从 URL 中提取仓库名(如https://github.com/user/repo.git→repo),输出文件为<repo-name>.bundle -
克隆镜像:执行
git clone --mirror "$URL" repo.git -
创建 bundle:进入
repo.git目录,执行git bundle create "../$BUNDLE" --all --tags,将所有分支和标签打包 -
验证 bundle:仍在
repo.git目录内,执行git bundle verify "../$BUNDLE"确认归档文件完整可用(git bundle verify必须在 git 仓库中运行) -
清理临时文件:退出临时目录,执行
rm -rf repo.git删除克隆的镜像仓库 -
报告结果:告知用户 bundle 文件已生成,输出文件路径和验证结果
注意事项
- 确保目标机器上已安装 git 且可访问远端仓库
- 生成的 bundle 文件位于当前工作目录
- 该文件可用于
git clone <bundle-file>还原仓库