How can I view all the git repositories on my machine?

Git

Git Problem Overview


Is there a way in which I can see all the git repositories that exist on my machine? Any command for that?

Git Solutions


Solution 1 - Git

If you are in Linux find / -name ".git", otherwise there is no way, they are standard directories, just use your OS file/folder find program to find .git named folders.

Solution 2 - Git

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.

Solution 3 - Git

On *nix, this will also find any --bare repositories.

find / -name "*.git" -type d

Solution 4 - Git

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>&-' \;

(edit: I thought the .git/config file was necessary, turns out it's not, so the absolute minimum git init newrepo is

mkdir -p newrepo/.git/{objects,refs}
echo ref: refs/heads/master >newrepo/.git/HEAD

)

Solution 5 - Git

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

Solution 6 - Git

On Linux, a faster way would be:

locate -r "\.git$"

assuming you keep locate's database updated with sudo updatedb

Solution 7 - Git

A simple PowerShell version:

Get-ChildItem . -Recurse -Hidden .git

Solution 8 - Git

On Linux, try this command with root permission:

find / | grep \\.git$

this just searchs every files that end with .git ... you can do it with searching tools in Windows, Linux etc...

Solution 9 - Git

For Linux:

dir="/home/${USER}"
dir_not="${dir}/miniconda3"
find /home/aeug -type d -iname ".git" -o -path "${dir_not}" -prune | xargs -0 echo 

Solution 10 - 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 }

I use this command at the beginning of the day. It simply adds a few git commands to the above. For some reason, our git repository works best if one runs a fetch then pull, don't know why. And we have a lot of submodules for some reason. Anyway, put what you need in between the {}'s.

push-location; Get-ChildItem . -Attributes Directory+Hidden -ErrorAction SilentlyContinue -Filter ".git" -Recurse | % { cd $_.parent.fullname; write-host '*************'; $(get-location).path; git fetch; git pull; git checkout .; git clean -f; git submodule update; git status; write-host '*************'; write-host ' '; }; pop-location

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionn1kh1lpView Question on Stackoverflow
Solution 1 - GitArkaitz JimenezView Answer on Stackoverflow
Solution 2 - Gitbopapa_1979View Answer on Stackoverflow
Solution 3 - GitgahooaView Answer on Stackoverflow
Solution 4 - GitjthillView Answer on Stackoverflow
Solution 5 - GitWalter TrossView Answer on Stackoverflow
Solution 6 - GitErnestoView Answer on Stackoverflow
Solution 7 - GitJulien BrdyView Answer on Stackoverflow
Solution 8 - GitMichel Gokan KhanView Answer on Stackoverflow
Solution 9 - GitInLawView Answer on Stackoverflow
Solution 10 - GitDisplay nameView Answer on Stackoverflow