Last active
June 20, 2019 02:45
-
-
Save themattgabriel/941db83087dc38a2f9d519c07b1e391c to your computer and use it in GitHub Desktop.
Update multiple repositories at the same time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Updates multiple repositories at once | |
# Use this site for reference regarding Bash colors: https://misc.flogisoft.com/bash/tip_colors_and_formatting | |
# MAKE SURE PATH IS CORRECT!!! | |
CDPATH="CHANGE_TO_PATH" # <--- Put the path to your local repositories here | |
SUCCESS="echo -e \\n\\e[92mLocal repositories updated\\e[0m\\n" | |
cd "$CDPATH" || return | |
# Find all repositories and update each of them to the latest revision | |
for i in $(find . -name ".git" | cut -d'/' -f2); do | |
echo | |
echo -e "\\e[33m$i\\e[0m" | |
# Switch to the repository | |
cd "$i" || return | |
# Check which branch the HEAD is on in the upstream repository | |
UPSTREAM_BRANCH="$(git remote show origin | sed -n 4p | cut -c 16-)" | |
if [ "$UPSTREAM_BRANCH" = "$ORIGIN_BRANCH" ]; then | |
# Pull the MASTER branch from the upstream repository | |
UPSTREAM_PULL="git pull --rebase upstream" | |
$UPSTREAM_PULL $ORIGIN_BRANCH | |
# Cleanup unnecessary files and optimize the local repository // Source: man git-gc | |
git gc --auto | |
# Push to your remote fork | |
git push -u origin --all --force # <--- Remove the `--force` option if destination branch is protected | |
else | |
echo "Branch is not the same" | |
echo "HEAD is at branch '$UPSTREAM_BRANCH'" | |
fi | |
# Return to $CDPATH | |
cd "$CDPATH" || return | |
done | |
echo | |
$SUCCESS | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment