How to automatically update all git repos in a folder
As someone who has many github repo folders on their server I wanted an easy way to update all of them regularly with minimal points of failure so I made this script:
#!/bin/bash
# store the current dir
CUR_DIR=$(pwd)
# Let the person running the script know what's going on.
echo "\nPulling in latest changes for all repositories...\n"
# Find all git repositories and update it to the master latest revision
for i in $(find . -name ".git" | cut -c 3-); do
echo "";
echo ""$i"";
# We have to go to the .git parent directory to call the pull command
cd "$i";
cd ..;
# finally pull
# the old name
git pull origin master;
# the new PC friendly name
git pull origin main;
# lets get back to the CUR_DIR
cd $CUR_DIR
done
echo "Complete!"
I have it run automatically every 10 minutes using cron
*/10 * * * * cd ~/ && bash ~/update_git_repos.sh
Obviously it would be more ideal if it only ran when a repo was updated but like I said, I wanted less points of failure.