Skip to content

Instantly share code, notes, and snippets.

@mshuler
Last active August 29, 2015 14:04
Show Gist options
  • Save mshuler/0cddeec5bb7f46dadbec to your computer and use it in GitHub Desktop.
Save mshuler/0cddeec5bb7f46dadbec to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Update all the git repositories under a directory.
#
################
# ToDo: handle multiple remotes?
################
#
# If script is placed in your $PATH, then hardcode REPODIR,
# or just drop it in the desired REPODIR (good for updating a specific REPODIR).
#
#REPODIR=~/git/
REPODIR="$( dirname $0 )"
if [ ! -d $REPODIR ]; then
echo "$REPODIR not found.."
exit 1
fi
################
#
# Find all the git repositories under REPODIR.
#
REPOSITORIES=$( find ${REPODIR} -maxdepth 1 -mindepth 1 -exec test -d {}/.git \; -print -prune | sort )
if [ -z "${REPOSITORIES}" ]; then
echo "No git repositories found in ${REPODIR}.."
exit 1
fi
################
################
co_pull () {
# check out each locally tracked branch and pull
for branch in $( git for-each-ref --format='%(refname:short)' refs/heads/ ); do
git checkout ${branch}
git pull
git fetch --tags
done
# prune branches that were deleted from the remote
git remote prune origin
# set CLEAN="true" in env to git clean
if [ "${CLEAN}" = "true" ]; then
git clean -xdf
fi
}
for repo in ${REPOSITORIES}; do
(
cd ${repo}
echo "=== Updating ${repo} ==="
co_pull
)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment