Skip to content

Instantly share code, notes, and snippets.

@MartinHarding
Created October 27, 2017 18:31
Show Gist options
  • Save MartinHarding/7c553963476e0f76c5acf3933f7fe06a to your computer and use it in GitHub Desktop.
Save MartinHarding/7c553963476e0f76c5acf3933f7fe06a to your computer and use it in GitHub Desktop.
A simple bash script to run git pull in a directory of repos
#!/usr/bin/env bash
# A simple bash script to run git pull in a directory of repos. Automatically
# detects whether each directory is a git repository, and can optionally
# checkout a branch before pulling using the `-b <branch-name>` argument.
# Save original working directory
_ORIGINAL_PWD="$PWD"
# Parse args
while getopts ":b:" opt; do
case $opt in
b) SYNC_BRANCH=$OPTARG;;
\?) echo "Invalid option: -$OPTARG" >&2;;
esac
done
# Find all git repos in the current directory or pull the current directory
REPOS_DIR="$PWD"
if [ -d "$REPOS_DIR/.git" ]; then
REPOS="$REPOS_DIR"
else
REPOS="`find $REPOS_DIR -name .git -maxdepth 2 -type d`"
fi
# CD into each directory, optionally checkout a branch, and pull
for REPO in $REPOS; do
echo "Syncing $REPO..."
cd $REPO && cd ..
[ $SYNC_BRANCH ] && git checkout $SYNC_BRANCH
git pull
done
# Go back to the original directory
cd $_ORIGINAL_PWD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment