On *nix, this will also find any --bare
repositories:
find / -name "*.git" -type d
On Linux, try this command with root permission:
find / | grep \\.git$
This just searches every file that ends with .git ... you can do it with searching tools in Windows, Linux, etc.:
ORIGINAL ANSWER: This works pretty well from Windows Powershell:
Get-ChildItem . -Attributes Directory+Hidden -ErrorAction SilentlyContinue -Include ".git" -Recurse
EDIT #1: -Filter is twice as fast as -Include. Here is that solution:
Get-ChildItem . -Attributes Directory+Hidden -ErrorAction SilentlyContinue -Filter ".git" -Recurse
EDIT #2: Keith E. Truesdell mentioned sending the output to a file. See his comment for that solution. I prefer console output. But his comment got me thinking that I prefer just the full path, not the whole mess that is returned by default. If you want that just the full path, use the following:
Get-ChildItem . -Attributes Directory+Hidden -ErrorAction SilentlyContinue -Filter ".git" -Recurse | % { Write-Host $_.FullName }
FINAL NOTE: The above solutions only return Git repositories under the current directory. If you want ALL repositories on a drive, you should run the command once from the root of each drive.
On Linux, a faster way would be:
locate -r "\.git$"
assuming you keep locate's database updated with sudo updatedb
Git repositories all have HEAD
, refs
and objects
entries.
on GNU/anything,
find -name HEAD -execdir test -e refs -a -e objects \; -printf %h\\n
Just checking for .git
will miss many bare repos and submodules.
To go full-paranoid on the checking you can ask git to do all its own checks before printing,
find -name HEAD -execdir test -e refs -a -e objects \; \
-execdir sh -ec 'GIT_DIR=$PWD git rev-parse --absolute-git-dir 2>&-' \;
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
For Linux:
dir="/home/${USER}"
dir_not="${dir}/miniconda3"
find /home/aeug -type d -iname ".git" -o -path "${dir_not}" -prune | xargs -0 echo
A simple PowerShell version:
Get-ChildItem . -Recurse -Hidden .git
Small variation from Eric Burcham's answer. That answer adds \.git to end, this one doesn't.
Get-ChildItem . -Attributes Directory+Hidden -ErrorAction SilentlyContinue -Filter ".git" -Recurse | % { Write-Host $_.Parent.FullName }
Ubuntu
find catalogue/archaeology/ -path '*/objects' -execdir git -C '{}' rev-parse --git-dir \; 2>&-
The simplest way was not yet listed: git config --get-all safe.directory
I wanted the answer to this and for it to be as fast as possible. In particular I want to search a home directory for workspaces with the following constraints:
- home directories may have VMWare shared mounts in them. No need to search these
- some directories are inaccessible to the user. These can be skipped
- directory names starting with
.
can be skipped - nested git workspaces are can be skipped
With this in mind, I am using this find
command:
find . \
-mount \
! -type d -prune -o \
! -executable -prune -o \
-name '.?*' -prune -o \
-execdir test -f '{}/.git/HEAD' \; -print -prune
This takes care to prune directories from the search as early as possible. Also, once a directory is positively confirmed as a git workspace, then it too is pruned. This returns the list of workspaces almost immediately (34ms on my home directory).