Skip to content

Instantly share code, notes, and snippets.

@ravidorr
Created April 12, 2022 08:49
Show Gist options
  • Save ravidorr/d7c301b4937c602ee26d78d06d9570a4 to your computer and use it in GitHub Desktop.
Save ravidorr/d7c301b4937c602ee26d78d06d9570a4 to your computer and use it in GitHub Desktop.
Scan the subdirectories for git repository, fetch, stash changes, switch to master, pull, switch back to original branch and pop stash
#!/usr/bin/env zsh
# change this to the directory containing your repositories
DIR=~/dev
CURRENT_DIR=`pwd`
for repo in "${DIR}/"*; do
if [[ -d "${repo}/.git" ]] then
cd "${repo}"
echo "\033[1mrepo: $repo\033[0m"
echo "fetch"
git fetch
# our DEFAULT_BRANCH are always master, so no need to check what it is.
# if it will change use this:
# DEFAULT_BRANCH=`git remote show git remote | sed -n '/HEAD branch/s/.*: //p'`
DEFAULT_BRANCH="master"
echo "default branch: ${DEFAULT_BRANCH}"
CURRENT_BRANCH=`git branch --show-current`
echo "current branch: ${CURRENT_BRANCH}"
echo "git stash"
git stash
if [[ "$DEFAULT_BRANCH" != "$CURRENT_BRANCH" ]]; then
echo -e "git checkout ${DEFAULT_BRANCH}"
git checkout $DEFAULT_BRANCH
fi
echo "git pull"
git pull
if [[ "$DEFAULT_BRANCH" != "$CURRENT_BRANCH" ]]; then
echo -e "git checkout ${CURRENT_BRANCH}"
git checkout $CURRENT_BRANCH
fi
echo "git stash pop"
git stash pop
fi
done
cd "${CURRENT_DIR}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment