Is there a way in which I can see all the git repositories that exist on my machine? Any command for that?
-
3See git-summary. It is a bash script that both lists all repositories and outputs their status information. Disclaimer, I am one of the devs.– cagliari2005Jan 18, 2018 at 16:46
-
@cagliari2005: What about bare repositories? stackoverflow.com/questions/60064170/…– LucianoFeb 4, 2020 at 19:26
11 Answers
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.
-
2On 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.– cjstehnoJan 7, 2010 at 14:07
-
3On Macs, the above command works too. (Unless you're running OS9 or earlier!) Jan 7, 2010 at 14:46
-
1Any 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.– jmlaneJun 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 -
1find 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
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.
-
1
-
2Get-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
-
1Very 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
On *nix, this will also find any --bare
repositories.
find / -name "*.git" -type d
-
4Bare 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 -
3Given that bare repositories don't need to follow that naming convention, is there a universal way to
find
those repos?– jmlaneJun 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
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
)
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
istrue
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 istrue
if the return status is0
(i.e., OK). The current node is available as{}
(which needs no escaping)-prune
is alwaystrue
, and causes all child nodes to be skipped-type d
istrue
for directories-print
is needed here because if-exec
is present it is not implicitly appended
-
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 adirname
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
On Linux, a faster way would be:
locate -r "\.git$"
assuming you keep locate's database updated with sudo updatedb
-
Definitely, locate is faster, use it with precautions, see here: unix.stackexchange.com/questions/60205/… Dec 20, 2017 at 6:24
A simple PowerShell version:
Get-ChildItem . -Recurse -Hidden .git
-
2This 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
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
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...
-
7There is no point letting
find
output everything then filtering withgrep
. I would rather use--name "*.git"
Jan 7, 2010 at 14:18 -
2
-
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 -
2any 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
For Linux:
dir="/home/${USER}"
dir_not="${dir}/miniconda3"
find /home/aeug -type d -iname ".git" -o -path "${dir_not}" -prune | xargs -0 echo
Ubuntu
find catalogue/archaeology/ -path '*/objects' -execdir git -C '{}' rev-parse --git-dir \; 2>&-