Find all local git repositories

Source: Stackoverflow

On Linux and OS X the following command is possibly the fastest (ignoring repositories without .git) when the root directory of find is /:

find / -name .git -exec dirname {} \; -prune

But for roots that have mostly repositories underneath, the following is probably the fastest (you may want to replace / with . or another root):

find / -type d -exec test -d {}/.git \; -prune -print

Quick explanation of the primaries of find used (since no operators are present here, -and is implicit, i.e., for each visited node primaries are evaluated left to right until one of them evaluates to false):

  • -name is true if the name matches (often, but not here, with wildcards)
  • -exec executes a command terminated by ; (which is escaped by \ to avoid interpretation by the shell), and is true if the return status is 0 (i.e., OK). The current node is available as {} (which needs no escaping)
  • -prune is always true, and causes all child nodes to be skipped
  • -type d is true for directories
  • -print is needed here because if -exec is present it is not implicitly appended