Have you ever needed to zip a git repository?

Well, this is fairly simple with git

git archive --format zip --output <output_path> HEAD

But what about if you need to zip multiple repositories at once? This might get tedious.

Bash scripting to the rescue

Worry no more!

Create a repoZipper.sh file with the following content

#!/bin/bash

# Set the name of the output archive file
OUTPUT_ARCHIVE="repos.zip"

# Create a temporary directory to store the cloned repositories
TEMP_DIR=$(mktemp -d)
echo "Working temporary directory: ${TEMP_DIR}"

# Loop through each repository URL passed as input
while read -r REPO_URL
do
  # Extract the repository name from the URL
  REPO_NAME=$(echo "${REPO_URL}" | cut -d'/' -f2)

  # Clone the repository
  git clone "${REPO_URL}" "${TEMP_DIR}/${REPO_NAME}"

  # Create a zip archive of the repository
  pushd "${TEMP_DIR}/${REPO_NAME}" || exit
  git archive --format=zip --output="${TEMP_DIR}/${REPO_NAME}.zip" HEAD
  popd || exit
done

# Combine all individual repos into one archive
pushd "${TEMP_DIR}" || exit
zip -r "${OUTPUT_ARCHIVE}" ./*.zip
popd || exit

# Move the output archive to the current directory
mv "${TEMP_DIR}/${OUTPUT_ARCHIVE}" .

# Remove the temporary directory
rm -rf "${TEMP_DIR}"

Then create a file repos.txt with the list of repositories to zip

Note: make sure the repository format is SSH (starting with git@ rather than https) otherwise the script will fail.

git@github.com:<username>/<repo_1>.git
git@github.com:<username>/<repo_2>.git

Now all is left to do is to call the script

bash repoZipper.sh < repos.txt

The script will:

  • create a temporary directory
  • clone all the repositories in the repos.txt file
  • zip each repository
  • zip all the zipped repositories into a single repos.zip archive
  • copy the repos.zip archive to the current directory
  • remove the temporary directory

Simple but effective!