Skip to content

Instantly share code, notes, and snippets.

@LakithKarunaratne
Created June 21, 2024 11:23
Show Gist options
  • Save LakithKarunaratne/ccb530a97098b5d7f1e230721ae9f0f3 to your computer and use it in GitHub Desktop.
Save LakithKarunaratne/ccb530a97098b5d7f1e230721ae9f0f3 to your computer and use it in GitHub Desktop.
Auto Pull List of Repos defined in a text file
#!/bin/bash
# Define the host alias
host_alias="github.com" # host alias as defined in .ssh/config
branch_name="latest" # name of the working branch
group_name="any" # name of the user or corp or group
# Assuming the text file is named repos.txt and is located in the current directory
while IFS= read -r repo
do
# Extract the repo name
repo_name=$(basename "$repo" .git)
# Check if the repo already exists
if [ -d "$repo_name" ]; then
# If the repo exists, update it
echo "Updating $repo_name"
cd "$repo_name"
git pull
cd ..
timestamp=$(date +%s)
echo "$timestamp: $repo_name was updated " >> log.txt
else
# If the repo doesn't exist, clone it
echo "Cloning $repo_name"
git clone "$host_alias:$group_name/$repo"
timestamp=$(date +%s)
echo "$timestamp: $repo_name was cloned " >> log.txt
fi
done < repos.txt
while IFS= read -r repo
do
# Extract the repo name
repo_name=$(basename "$repo" .git)
# Check if the repo exists
if [ -d "$repo_name" ]; then
# If the repo exists, update it
echo "Switching $repo_name Branch to $branch_name "
cd "$repo_name"
git checkout $branch_name
cd ..
timestamp=$(date +%s)
echo "$timestamp: $repo_name was switched to branch $branch_name " >> log.txt
else
# If the branch doesn't exist, announce it
echo "No such branch: $branch_name"
timestamp=$(date +%s)
echo "$timestamp: $repo_name has no branch $branch_name " >> log.txt
fi
done < repos.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment