Skip to content

Instantly share code, notes, and snippets.

@Gerst20051
Forked from mzabriskie/README.md
Last active May 14, 2018 19:52
Show Gist options
  • Save Gerst20051/81d6141ee8f84dc4f15e5d286e3ef452 to your computer and use it in GitHub Desktop.
Save Gerst20051/81d6141ee8f84dc4f15e5d286e3ef452 to your computer and use it in GitHub Desktop.
Check Git Status For Directory Of Repos
#!/bin/bash
function git_status_directory {
dir="$1"
if [ -z "$dir" ]; then # No directory has been provided, use current
dir="`pwd`"
fi
if [[ $dir != */ ]]; then # Make sure directory ends with '/'
dir="$dir/*"
else
dir="$dir*"
fi
echo
for f in $dir; do # Loop through all sub-directories
[ -d "${f}" ] || continue # Skip anything not a directory
echo -en "\033[0;35m"
echo "${f}"
echo -en "\033[0m"
if [ -d "$f/.git" ]; then # Check if directory is a git repository
modified=0
\cd $f
if [ $(git status | grep modified -c) -ne 0 ]; then # Check for modified files
modified=1
echo -en "\033[0;31m"
echo 'Modified files'
echo -en "\033[0m"
fi
if [ $(git status | grep Untracked -c) -ne 0 ]; then # Check for untracked files
modified=1
echo -en "\033[0;31m"
echo 'Untracked files'
echo -en "\033[0m"
fi
if [ $modified -eq 0 ]; then # Check if nothing to commit
echo 'Nothing to commit'
else
git status
fi
\cd ../
else
echo 'Not a git repository'
fi
echo
done
}

If you're like me you have a directory like ~/Workspace/Github where all your git repos live. I often find myself making a change in a repo, getting side tracked and ending up in another repo, or off doing something else all together. After a while I end up with several repos with modifications. This script helps me pick up where I left off by checking the status of all my repos, instead of having to check each one individually.

Usage:

git_status_directory [directory]

This will run git status on each repo under the directory specified. If called with no directory provided it will default to the current directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment