如何查看我机器上的所有 git 存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2020812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:38:57  来源:igfitidea点击:

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

git

提问by n1kh1lp

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

有没有办法可以看到我机器上存在的所有 git 存储库?有什么命令吗?

采纳答案by Arkaitz Jimenez

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

如果您在 Linux 中find / -name ".git",否则没有办法,它们是标准目录,只需使用您的操作系统文件/文件夹查找程序来查找.git命名文件夹。

回答by bopapa_1979

ORIGINAL ANSWER: This works pretty well from Windows Powershell:

原始答案:这在 Windows Powershell 中运行良好:

Get-ChildItem . -Attributes Directory+Hidden -ErrorAction SilentlyContinue -Include ".git" -Recurse

EDIT #1: -Filter is twice as fastas -Include. Here is that solution:

编辑#1:-Filter 是-Include 的两倍。这是解决方案:

Get-ChildItem . -Attributes Directory+Hidden -ErrorAction SilentlyContinue -Filter ".git" -Recurse

EDIT #2: Keith E. Truesdellmentioned 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:

编辑 #2Keith E. Truesdell提到将输出发送到文件。请参阅他对该解决方案的评论。我更喜欢控制台输出。但是他的评论让我觉得我更喜欢完整路径,而不是默认返回的整个混乱。如果您只想要完整路径,请使用以下命令:

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.

最后说明:上述解决方案仅返回当前目录下的 Git 存储库。如果您希望驱动器上的所有存储库,您应该从每个驱动器的根目录运行该命令一次。

回答by gahooa

On *nix, this will also find any --barerepositories.

在 *nix 上,这也会找到任何--bare存储库。

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

回答by jthill

Git repositories all have HEAD, refsand objectsentries.

Git 存储库都有HEAD,refsobjects条目。

on GNU/anything,

在 GNU/任何东西上,

find -name HEAD -execdir test -e refs -a -e objects \; -printf %h\n

Just checking for .gitwill miss many bare repos and submodules.

仅仅检查.git就会错过许多裸仓库和子模块。

To go full-paranoid on the checking you can ask git to do all its own checks before printing,

要在检查中完全偏执,您可以要求 git 在打印之前进行所有自己的检查,

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/configfile was necessary, turns out it's not, so the absolute minimum git init newrepois

(编辑:我认为该.git/config文件是必要的,事实证明它不是,所以绝对最小值git init newrepo

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

)

)

回答by Ernesto

On Linux, a faster way would be:

在 Linux 上,更快的方法是:

locate -r "\.git$"

locate -r "\.git$"

assuming you keep locate's database updated with sudo updatedb

假设您保持 locate 的数据库更新 sudo updatedb

回答by Walter Tross

On Linux and OS X the following command is possibly the fastest (ignoring repositories without .git) when the root directory of findis /:

在Linux和OS X下面的命令可能是最快的(忽略不存储库.git)时的根目录find/

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 primariesof findused (since no operatorsare present here, -andis implicit, i.e., for each visited node primariesare evaluated left to right until one of them evaluates to false):

对used的原色的快速解释find(因为这里没有运算符,所以-and是隐式的,即,对于每个访问过的节点,从左到右计算原色,直到其中一个计算为false):

  • -nameis trueif the name matches (often, but not here, with wildcards)
  • -execexecutes a command terminated by ;(which is escaped by \to avoid interpretation by the shell), and is trueif the return status is 0(i.e., OK). The current node is available as {}(which needs no escaping)
  • -pruneis always true, and causes all child nodes to be skipped
  • -type dis truefor directories
  • -printis needed here because if -execis present it is not implicitly appended
  • -nametrue如果名称匹配(通常,但不在这里,使用通配符)
  • -exec执行由终止的命令;(它被转义\以避免被shell解释),并且true如果返回状态是0(即OK)。当前节点可用{}(不需要转义)
  • -pruneis always true,并导致跳过所有子节点
  • -type dtrue对目录
  • -print此处需要,因为如果-exec存在,则不会隐式附加

回答by Michel Gokan

On Linux, try this command with root permission:

在 Linux 上,使用 root 权限尝试此命令:

find / | grep \.git$

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

这只是搜索以 .git 结尾的每个文件......你可以使用 Windows、Linux 等中的搜索工具来完成......

回答by Julien Brdy

A simple PowerShell version:

一个简单的 PowerShell 版本:

Get-ChildItem . -Recurse -Hidden .git

回答by Display name

Small variation from Eric Burcham's answer. That answer adds \.git to end, this one doesn't.

与 Eric Burcham 的回答略有不同。该答案将 \.git 添加到结尾,而这个答案没有。

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.

我在一天开始时使用这个命令。它只是在上面添加了一些 git 命令。出于某种原因,我们的 git 存储库在运行 fetch 然后拉取时效果最佳,不知道为什么。出于某种原因,我们有很多子模块。无论如何,将您需要的放在 {} 之间。

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

回答by InLaw

For Linux:

对于 Linux:

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