Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export all Git branches into individual zip files for each branch

Tags:

git

export

I am looking for a way to export a Git repository so that I get a zip or directory for each branch individually.

This is similar to Do a "git export" (like "svn export")?, but instead of a zip of the current branch I am hoping to get a zip for every branch.

Is there a Git or Bash command that could do this?

like image 373
Kyeotic Avatar asked Sep 04 '25 01:09

Kyeotic


1 Answers

Use git archive with a branch argument

git archive can accept a branch name to export as an argument, so you just need to write a script that loops through the branch names and passes them to git archive. For example, this will work in 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

I modified it from the 2nd example in the official Linux Kernel documentation for git for-each-ref.

Breakdown

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

git for-each-ref will list each local branch under refs/heads, using just the branch name with the refname:short format.

That output is piped into a Bash while loop, which then substitutes the branch name for the git archive arguments:

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

For-loop solution

Here's a Bash for loop solution, inspired by toydarian's (non-working) solution:

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

Documentation

  • git-for-each-ref(1) Manual Page
  • git-archive(1) Manual Page

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!