Skip to content

Instantly share code, notes, and snippets.

@adrianoluis
Last active June 13, 2024 01:51
Show Gist options
  • Save adrianoluis/5b07cbf5bbc3679efff0b45b9d98c1cd to your computer and use it in GitHub Desktop.
Save adrianoluis/5b07cbf5bbc3679efff0b45b9d98c1cd to your computer and use it in GitHub Desktop.
Update master in all git repo under the provided directory or if missing uses running directory.
#!/bin/sh
GITROOT="${1:-.}"
find $GITROOT -name .git -type d | cat -n | while read n f
do
cd "$(dirname "$f")"
GITDEFBR="$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')"
GITPROJ="$(pwd -P)"
echo "\033[32m>>> \033[1m$GITPROJ\033[0m"
GITBRANCH=$(git rev-parse --abbrev-ref HEAD)
git add .
echo "> Saving local changes: $(git stash)"
if [ ! "$GITBRANCH" = "$GITDEFBR" ]
then
echo "> Swithing to '$GITDEFBR'"
git checkout $GITDEFBR &>/dev/null
fi
echo "> Fetching latest from repo: $(git pull --rebase)"
echo '> Cleaning up unnecessary files and optimizing the local repository'
git clean -df
echo '> Prunning all unaccessible objects'
git gc --aggressive
git remote prune origin
if [ ! "$GITBRANCH" = "$GITDEFBR" ]
then
echo "> Swithing back to '$GITBRANCH'"
git checkout $GITBRANCH &>/dev/null
fi
if [ ! "$(git stash list)x" = 'x' ]
then
echo '> Restoring previous saved changes.'
git stash pop
git reset HEAD .
fi
cd - &>/dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment