Last active
January 21, 2022 17:19
-
-
Save kenaniah/7af6efe09df138a8a2b9 to your computer and use it in GitHub Desktop.
Git Branch auto-clone script
This file contains hidden or 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
# Git branch auto-clone script | |
# https://gist.github.com/kenaniah/7af6efe09df138a8a2b9 | |
# BASE should point to a bare git repo | |
# DEST should point to a directory for branches to be checked out into | |
# Define where things are pulled from | |
BASE=$1 | |
DEST=$2 | |
# Fetch to the source repo | |
echo "Fetching from source..." | |
git --git-dir=$BASE fetch | |
# List branches | |
BRANCHES=$(git --git-dir=$BASE branch -r | grep -v HEAD) | |
# Ensure all branches are tracked | |
for i in $BRANCHES; do | |
git --git-dir=$BASE branch -f ${i#*/} origin/${i#*/} > /dev/null; | |
done; | |
# Clone or update branch | |
for BRANCH in $BRANCHES; do | |
echo "Processing branch: $BRANCH" | |
DIR=`echo "$BRANCH" | cut -d "/" -f 2` | |
if [ -d "$DEST/$DIR" ]; then | |
cd $DEST/$DIR | |
git pull | |
cd - > /dev/null | |
else | |
git clone --local --single-branch --branch $DIR $BASE "$DEST/$DIR" | |
fi; | |
done; | |
# Find branches to delete | |
STALE=`git --git-dir=$BASE fetch -p 2>&1 | grep "deleted" | rev | cut -d " " -f 1 | rev` | |
for BRANCH in $STALE; do | |
echo "Deleting branch: $BRANCH" | |
DIR=`echo "$BRANCH" | cut -d "/" -f 2` | |
if [ -d "$DEST/$DIR" ]; then | |
rm -rf $DEST/$DIR | |
fi; | |
done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment