Skip to content

Instantly share code, notes, and snippets.

@abdul-alhasany
Created November 9, 2022 00:22
Show Gist options
  • Save abdul-alhasany/31e0932998d3481cb5ce2f6438aa7816 to your computer and use it in GitHub Desktop.
Save abdul-alhasany/31e0932998d3481cb5ce2f6438aa7816 to your computer and use it in GitHub Desktop.
Pull all branches from remote and copy into separate folder for each one
#https://stackoverflow.com/a/65020975
git fetch --all --prune
# reset branch only if it has deviated from the remote
function update_branch {
if git diff --exit-code --quiet $1; then
echo "No changes on branch $1"
else
echo "Resetting branch $1"
git reset --hard $1
fi
}
# master branch is checked out in the root
update_branch origin/master
# check out each remote branch into a subfolder
branches=$(git for-each-ref --format='%(refname:short)' refs/remotes/origin | grep -v /HEAD | grep -v /master)
for branch in $branches; do
if [ -d $branch ]; then
cd $branch
update_branch $branch
cd $OLDPWD
else
echo "Checking out new branch $branch"
git worktree add $branch $branch
fi
done
# check for branches that have been deleted on the remote
for branch in origin/*; do
if ! git show-ref --verify refs/remotes/$branch --quiet; then
echo "Removing worktree $branch"
git worktree remove $branch
fi
done
git worktree prune
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment