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

前端 未结 10 779
南旧
南旧 2020-11-29 21:00

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

相关标签:
10条回答
  • 2020-11-29 21:06

    A simple PowerShell version:

    Get-ChildItem . -Recurse -Hidden .git
    
    0 讨论(0)
  • 2020-11-29 21:07

    On Linux, a faster way would be:

    locate -r "\.git$"

    assuming you keep locate's database updated with sudo updatedb

    0 讨论(0)
  • 2020-11-29 21:17

    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
    
    0 讨论(0)
  • 2020-11-29 21:18

    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
    0 讨论(0)
  • 2020-11-29 21:22

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

    find / -name "*.git" -type d
    
    0 讨论(0)
  • 2020-11-29 21:22

    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
    

    )

    0 讨论(0)
提交回复
热议问题