Skip to content

Instantly share code, notes, and snippets.

@xzel23
Last active April 27, 2018 10:53
Show Gist options
  • Save xzel23/871cf385de8d64edbd18f9f4f5d59707 to your computer and use it in GitHub Desktop.
Save xzel23/871cf385de8d64edbd18f9f4f5d59707 to your computer and use it in GitHub Desktop.
run a git command on all subdirectories containing a git repository (tested on MAC, should also work on linux - report problems via comments)
#!/bin/bash
SCRIPT=`basename $0`
if [ $# -lt 1 ] || [ "$1" = "-help" ] || [ "$1" = "--help" ] ; then
cat -- <<EOF
NAME
${SCRIPT} - run git command for all directories
SYNOPSIS
${SCRIPT} [git-command] [git-arguments]
DESCRIPTION
${SCRIPT} checks all subdirectories of the current directory for presence of a git repository
and then running the supplied git-command.
Checking is done by searching all direct child directories of the current directory that contain
a subdirectory named '.git'.
EXAMPLES
${SCRIPT} status -s
output short status for each repository.
${SCRIPT} fetch
fetch changes for each repository.
EOF
exit
fi
# get arguments to git
ARGS=$*
# find and iterate over repositories
find . -type d -depth 2 -name .git -exec bash -c "
REPODIR='{}'
DIR=\${REPODIR/\/.git/}
DIR=\${DIR/^.\//}
printf '\n\033[1;34m%s\033[m\n' "\$DIR"
( cd "\$DIR" && git $ARGS )
" \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment