Member-only story
Archive Git repositories with Shell Script
Shell script to clone repositories at a specific branch, remove the .git folder, and have a clean, archived copy of the repository.
7 min readDec 25, 2022
Git Repositories and Branch Names
Recently, I needed to archive multiple Git repositories into a single Git repository. In addition, some of these archived repositories have multiple branches worth archiving. So, I needed to support a format like this:
"git@github.com:KenanBek/dbui.git;main,dev"
"git@github.com:KenanBek/django-skeleton.git;master"
Each line contains,
- Git URL;
- Comma (
,
) separate list of branch names;
Git URL and branch names are separated by a semicolon (;
).
Shell Script to Archive
The goal is to have a simple shell script that will automate the following steps:
- Parse a list in the above-provided format and get repository URL and branch names;
- Iterate over each repository and branch, check commit-hash;
- If there are changes, clone the repository at each branch (e.g., repo1-main, repo1-dev, repo2-main, etc.);
- Remove
.git
folder from each cloned repository subdirectory; - Commit and push;
Based on the above, we will have one git repository with archived content of listed repositories. The structure of the archive repository is as follows:
git-archive.sh # our shell script
repo1-main
repo1-dev
repo2-main
...
The Script
# parse the command-line arguments
while getopts ":nh" opt; do
case ${opt} in
h)
echo "Usage:"
echo " git-clone.sh [-n]"
echo ""
echo "Options:"
echo " -n Skip commit and push"
exit 0
;;
n)
# set a flag to skip the commit_and_push function
no_push=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# list of repositories with list of branches for each repository
repos=(
"git@github.com:KenanBek/dbui.git;main,dev"…