title: How to quickly find all git repos under a directory
body: <p>The following bash script is slow when scanning for .git directories because it looks at every directory. If I have a collection of large repositories it takes a long time for find to churn through every directory, looking for .git. It would go much faster if it would prune the directories within repos, once a .git directory is found. Any ideas on how to do that, or is there another way to write a bash script that accomplishes the same thing?</p>
  
  <pre><code>#!/bin/bash
  
  # Update all git directories below current directory or specified directory
  
  HIGHLIGHT="\e[01;34m"
  NORMAL='\e[00m'
  
  DIR=.
  if [ "$1" != "" ]; then DIR=$1; fi
  cd $DIR&gt;/dev/null; echo -e "${HIGHLIGHT}Scanning ${PWD}${NORMAL}"; cd -&gt;/dev/null
  
  for d in `find . -name .git -type d`; do
    cd $d/.. &gt; /dev/null
    echo -e "\n${HIGHLIGHT}Updating `pwd`$NORMAL"
    git pull
    cd - &gt; /dev/null
  done
  </code></pre>
  
  <hr>
  
  <p>Specifically, how would you use these options? For this problem, you cannot assume that the collection of repos is all in the same directory; they might be within nested directories.</p>
  
  <pre><code>top
    repo1
    dirA
  
    dirB
       dirC
          repo1
  </code></pre>