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?
git archive with a branch argumentgit 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.
$ 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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With