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