Skip to content

Instantly share code, notes, and snippets.

@melroy89
Created November 9, 2020 23:55
Show Gist options
  • Save melroy89/62988f2800dcaa12b0a2b844c9de4523 to your computer and use it in GitHub Desktop.
Save melroy89/62988f2800dcaa12b0a2b844c9de4523 to your computer and use it in GitHub Desktop.
Pull/push all your Git repos Bash script (Origin: https://gitlab.melroy.org/-/snippets/40)
#!/usr/bin/env bash
# Script that goes through all my git repos and update them. And report any untracked files.
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
NC='\033[0m' # No Color
# Do not add automatically to staged area, but only pull & push all repos
CURRENT="$(dirname "$(realpath "$0")")"
for dir in */;do
cd "$CURRENT/$dir"
echo "INFO: processing ${dir%/}"
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ ! -z $BRANCH ]; then
if [[ "$BRANCH" =~ ^(master|main)$ ]]; then
UNTRACKED_FILES=$(git ls-files --others --exclude-standard)
if [ ! -z "$UNTRACKED_FILES" ]; then
echo -e "${ORANGE}WARN:${NC} some files are untracked! List of untracked files:"
echo "$UNTRACKED_FILES"
fi
git pull --quiet && git push --quiet
ret=$?
if [ $ret -ne 0 ]; then
echo -e "${RED}ERROR:${NC} Something went wrong during git pull/push.\n"
else
echo -e "${GREEN}DONE:${NC} ${dir%/} is now up-to-date and pushed successfully."
fi
else
echo -e "${ORANGE}WARN:${NC} Skipping ${dir%/}, current branch is not master or main."
fi
else
echo -e "${ORANGE}WARN:${NC} Skipping ${dir%/}, directory has no git repo."
fi
echo "-----------"
done
@melroy89
Copy link
Author

melroy89 commented Nov 9, 2020

This gist is just a mirror from the one on GitLab. See my original snippet: https://gitlab.melroy.org/-/snippets/40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment