Find Git Repositories – How to Find All Git Repositories Within Given Folders

findgit

Naive approach is find dir1 dir2 dir3 -type d -name .git | xargs -I {} dirname {}
, but it's too slow for me, because I have a lot deep folder structures inside git repositories (at least I think that this is the reason). I've read about that I can use prune to prevent find to recurse into directories once it found something, but there's two things. I'm not sure how this works (I mean I don't understand what prune does although I've read man page) and the second it wouldn't work in my case, because it would prevent find to recurse into .git folder but not into all other folders.

So what I actually need is:

for all subdirectories check if they contain a .git folder and if it is then stop searching in this filesystem branch and report result. It would be perfect if this would also exclude any hidden directories from search.

Best Answer

Okay, I still don't totally sure how this works, but I've tested it and it works.

.
├── a
│   ├── .git
│   └── a
│       └── .git
└── b
    └── .git

6 directories, 0 files

% find . -type d -exec test -e '{}/.git' ';' -print -prune
./a
./b

I'm looking forward into making the same faster.

Related Question