169

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

2

11 Answers 11

131

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.

8
  • 2
    On Windows (and I am sure Mac too) you could do something similar... just a search for directories named .git - which is what git uses to store its meta information.
    – cjstehno
    Jan 7, 2010 at 14:07
  • 3
    On Macs, the above command works too. (Unless you're running OS9 or earlier!) Jan 7, 2010 at 14:46
  • 1
    Any good shell scripts or small utility applications that implement this simple file search functionality and add repository status information to the list? Shouldn't be an issue to write a shell script to do so, but rather use well-adopted scripts than my own unoptimized hacks.
    – jmlane
    Jun 10, 2011 at 17:06
  • 7
    @jmlane for d in `find / -name ".git"`; do cd $d/..; echo `pwd`:; git status; echo; done Sep 26, 2013 at 13:17
  • 1
    find man page has solution. Similar to the accepted answer but using -prune to return faster without going into the .git directories. Without prune find would go into the .git folder and depending on the contents spend time searching inside unnecessarily. Man page solution follows "find repo/ ( -exec test -d '{}'/.svn \; -or \ -exec test -d {}/.git \; -or -exec test -d {}/CVS \; ) \ -print -prune Check it out. Edit out svn and cvs if not required Oct 29, 2018 at 12:32
61

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.

9
  • 1
    very useful for windows users! :) Dec 2, 2016 at 15:50
  • 2
    Get-ChildItem : A parameter cannot be found that matches parameter name 'Attributes'. At line:1 char:28 Jan 30, 2017 at 9:33
  • 2
    @DewaldSwanepoel - You can DEFINITELY upgrade your PowerShell version. We do it with chocolatey. I am currently running PowerShell version 5.0 on Windows 7 at work. Mar 15, 2017 at 17:12
  • 1
    Very useful and quick. I like the suppression of errors. I would also add the output to a file and to filter for only the "directory". Get-ChildItem . -Attributes Directory,Directory+Hidden -ErrorAction SilentlyContinue -Include ".git" -Recurse | Out-File -FilePath C:\Dev\GitRepoList.txt Jun 3, 2019 at 19:42
  • 1
    @KeithE.Truesdell - I am pretty sure the "Directory+Hidden" attributes is inclusive, and will only search directories. Adding -Directory my be faster in newer versions of PowerShell. I'll go time it. EDIT - Adding the "-Directory" makes no material difference, as the documentation would indicate. However, adding the Directory Attribute as you suggest does make it slower. I'm have now idea why, but I tested it both ways with Measure-Command and all three with the additional Directory attribute were slower than all three without. Jun 5, 2019 at 19:47
23

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

find / -name "*.git" -type d
3
  • 4
    Bare repositories don't need to be named name.git thats just a convention, that I for example, don't follow. Jan 7, 2010 at 14:09
  • 3
    Given that bare repositories don't need to follow that naming convention, is there a universal way to find those repos?
    – jmlane
    Jun 10, 2011 at 17:04
  • Very old post, but you could adapt this and instead see if something like git rev-parse --git-dir succeeds. Aug 17, 2021 at 18:48
17

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

)

1
  • That last bit is new to me. Didn't know it was that simple Jul 9, 2021 at 5:13
10

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
3
  • I timed your and my version of the first find on a big tree, and yours was faster by 3.6% (46.6 seconds vs 48.4), so, yes, faster, but not “much faster” (on a MacBook Pro 2018 with Big Sur) Jan 18, 2022 at 14:03
  • The second find command is interesting because it doesn't print .git dirs that are nested inside a .git dir. I'm worried about its performance though. The first command can be made much faster with find / -name .git -exec dirname {} + -prune Jan 18, 2022 at 14:10
  • Was to late to edit my comment. There's no need of xargs. Only thing to change is replace \;with + so that a dirname process isn't started for every repository. You are right that in this case, I was wrong saying it's much faster. I tend to have a horror reflex everytime I see a find command not taking advantage of commands that accept file lists as arguments. Jan 18, 2022 at 14:15
5

On Linux, a faster way would be:

locate -r "\.git$"

assuming you keep locate's database updated with sudo updatedb

1
2

A simple PowerShell version:

Get-ChildItem . -Recurse -Hidden .git
1
  • 2
    This worked for me! Thank you! I did get some errors for a few locations it tried to access that running as a normal user the powershell script did not have access to, but upon further review, these were places I shouldn't be worried about anyway (ie - C:\users\<username>\PrintHood Also, I found it useful to add an output to a file and since I only cared about the path (as this script gets a bunch of info) to also filter only for the path/directory info. Get-ChildItem . -Recurse -Hidden .git | Out-file -FilePath C:\Dev\GitRepoList.txt Jun 3, 2019 at 19:26
2

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
1

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...

5
  • 7
    There is no point letting find output everything then filtering with grep. I would rather use --name "*.git" Jan 7, 2010 at 14:18
  • 2
    @Gregory Pakosz : What's the difference ? Jan 7, 2010 at 14:24
  • 14
    @Michel, you start 2 processes and make the first one transmit through a pipe the whole / tree for the second to grep, when the first one can do everything and avoid the huge useless IO use. Not a real difference for the user normally, but for big filesystems it might make a difference. Jan 7, 2010 at 14:39
  • 2
    any way, if you wanna use JUST find command, it's better to use -regex instead of -name ... in this case, use this command: sudo find / -regex '.*\.git' Jan 7, 2010 at 22:05
  • 5
    @MichelKogan better why?
    – Mark Amery
    Jun 30, 2014 at 21:04
1

For Linux:

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

Ubuntu

find catalogue/archaeology/ -path '*/objects' -execdir git -C '{}' rev-parse --git-dir \; 2>&-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.