git - 将所有 Git 分支导出到每个分支的单独 zip 文件中

标签 git export

我正在寻找一种导出 Git 存储库的方法,以便为每个分支单独获取一个 zip 或目录。

这类似于 Do a "git export" (like "svn export")? ,但不是 current 分支的 zip,我希望得到 every 分支的 zip。

是否有可以执行此操作的 Git 或 Bash 命令?

最佳答案

使用带有分支参数的 git archive

git archive 可以接受一个分支名称作为参数导出,所以你只需要编写一个脚本来循环遍历分支名称并将它们传递给 git archive .例如,这将适用于 Windows Git Bash:

git for-each-ref --format='%(refname:short)' refs/heads | \
while read branch; do
  git archive --format zip --output <outputDirectory>/${branch}.zip $branch
done

我从 official Linux Kernel documentation for git for-each-ref 中的第二个nd 示例修改了它。

分割

$ git for-each-ref --format='%(refname:short)' refs/heads
foo
master

git for-each-ref 将在 refs/heads 下列出每个本地分支,仅使用带有 refname:short 的分支名称格式。

该输出通过管道传输到 Bash while 循环中,然后用分支名称替换 git archive 参数:

while read branch; do
  git archive --format zip --output <outputDirectory>/${branch}.zip $branch
done

For循环解决方案

这是一个受 toydarian's (non-working) solution 启发的 Bash for 循环解决方案:

for branch in $(git for-each-ref --format='%(refname:short)' refs/heads); do
  git archive --format zip --output <outputDirectory>/${branch}.zip $branch
done

文档

关于git - 将所有 Git 分支导出到每个分支的单独 zip 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24370850/

相关文章:

mysql - 在 MySQL 中将规范化表导出为矩阵格式

mysql - 如何将数据库从 Amazon RDS MySQL 实例导出到本地实例?

git - 如何从远程存储库获取特定分支中的最后一次提交 SHA

Github错误: src refspec BRANCHNAME does not match any

git checkout -f master VS git reset

Github:推送 pull 请求

mongodb - 使用 MongoExport,如何控制日期字段的输出?

node.js - 导出mongodb集合数据并使用node js导入回来

git - checkout 远程分支时丢失本地提交

javascript - 如何使用正确的名称将Kotlin函数导出到Javascript