有没有办法在终端列出git存储库?

时间:2011-02-24 07:06:54

标签: git terminal

我的系统上有几个git存储库,我想在终端中列出所有这些存储库。

我正在寻找的是这样的:

/ path / to / my / git / repo / 1 / REPONAME1
/ path / to / my / git / repo / 2 / REPONAME2
/ path / to / my / git / repo / 3 / REPONAME3

如果你能想出如何显示分支名称和回购状态,例如。 [大师] c:0你:1 d:0,那会很棒。

5 个答案:

答案 0 :(得分:9)

要列出系统上的所有git存储库,可以在bash shell终端(在命令行)运行以下命令并找到它们。

find / -name .git -type d -exec dirname {} \;

答案 1 :(得分:5)

我发现这比find命令更准确,速度更快。我相信它可以改进,但这是一个好的开始。显然,在运行此命令之前,您的文件数据库需要是最新的。在Linux系统上,您可以通过运行“updatedb”来完成此操作。

locate -br '\.git$' | rev | cut -c 6- | rev

答案 2 :(得分:3)

lsgit是执行此操作的脚本。它本质上是locate -br '^HEAD$'的包装。

如果本地计算机上有多个同一仓库的克隆,则表明这些仓库是相关的。

答案 3 :(得分:3)

<块引用>

如果你能想出如何显示分支名称和仓库的状态

我们的想法是:

  • (首先)查找并注册存储库,
  • 然后使用新的(Git 2.30,2020 年第四季度)git for-each-repo command

首先,选择您想要的任何配置键名称。
例如:“list.repos

使用 git config --unset-all(在全局配置文件中)重新初始化列表:

git config --global --unset-all list.repos

然后使用:

将结果重定向到文件“list-repos.txt”中,然后注册这些存储库,reading each line of the file

while IFS="" read -r repo || [ -n "${repo}" ]
do
  git config --global --add list.repos ${repo}
done < list-repos.txt

一旦您的存储库被注册(作为全局 git 配置值),使用新命令 git for-each-repo 循环它们是微不足道的,执行您需要的任何命令。

在 Git 2.30(2020 年第 4 季度)中引入了一个新命令,最初用于管理“git maintenance(man) 的部分并简化 crontab 条目的编写(和其他调度系统配置)。

参见 commit 0016b61commit 61f7a38commit a4cb1a2(2020 年 10 月 15 日)、commit 2fec604commit 0c18b70commit 4950b2acommit b08ff1f (2020 年 9 月 11 日)和 commit 1942d48(2020 年 8 月 28 日)Derrick Stolee (derrickstolee)
(2020 年 11 月 18 日在 Junio C Hamano -- gitster --commit 7660da1 合并)

<块引用>

for-each-repo:在配置的存储库上运行子命令

签字人:Derrick Stolee

<块引用>

将存储库列表存储在全局或系统配置中,然后在该列表上迭代 Git 命令会很有帮助。
创建一个新的内置函数,使专家可以轻松完成此过程。
我们将使用此内置程序在未来的更改中对所有配置的存储库运行计划维护。

测试非常简单,但强调“--”参数是可选的。

git for-each-repo 现在包含在其 man page 中:

<块引用>

git-for-each-repo(1)

姓名

git-for-each-repo - 在存储库列表上运行 Git 命令

概要

[verse]
'git for-each-repo' --config=<config> [--] <arguments>

描述

对存储库列表运行 Git 命令。之后的争论 已知选项或 -- 指示符用作 Git 的参数 子进程。

此命令是实验性的。行为可能会改变。

例如,我们可以对存储库列表中的每一个进行维护 使用

存储在maintenance.repo配置变量中

git for-each-repo --config=maintenance.repo 维护运行

这将为每个值 git -C <repo> maintenance run 运行 <repo> 在多值配置变量 maintenance.repo 中。

选项

--config=<config>

使用给定的配置变量作为多值列表存储 绝对路径名。迭代要运行的路径列表 给定的参数。

这些配置值是从系统、全局和本地 Git 配置加载的, 可用。如果 git for-each-repo 运行的目录不是 Git 存储库,则仅使用系统和全局配置。

子进程行为

如果任何 git -C <repo> <arguments> 子进程返回非零退出代码, 然后 git for-each-repo 进程返回退出代码而不运行 更多子流程。

每个git -C <repo> <arguments>子进程继承标准文件 描述符 stdinstdoutstderr


对于 Git 2.30.1(2021 年第一季度),“git for-each-repo --config=<var> <cmd>(man) 不应在配置变量 { {1}} 甚至没有定义一次。

请参阅 commit 6c62f01Derrick Stolee (derrickstolee)(2021 年 1 月 8 日)。
(于 2021 年 1 月 15 日在 Junio C Hamano -- gitster --commit aa08688 合并)

<块引用>

for-each-repo:对空配置不做任何事情

报告人:Andreas Bühmann
帮助:Eric Sunshine
帮助:Junio C Hamano
签字人:Derrick Stolee

<块引用>

'git for-each-repo --config=X'(man) 应该在配置键 '<cmd>' 没有值时返回成功而不调用任何子命令。
当前的实现改为段错误。

如果用户使用“git maintenance start(man) 使用“git for-each-repo --config=maintenance.repo ... 来初始化他们的 cron 计划,他们可能会遇到这个问题(man) 但随后使用 'git maintenance unregister'(man) 删除配置选项。
(注意:'git maintenance stop'(man) 将删除配置删除 cron 计划。)

添加一个简单的测试以确保它有效。
使用 'git help --no-such-option'(man) 作为潜在的子命令,以确保在运行该子命令时我们会失败。

答案 4 :(得分:1)

一个包装器:
locate -br '^HEAD$' 由Dee Newcum https://github.com/DeeNewcum/dotfiles/blob/master/bin/lsgit完成,或者由https://gist.github.com/nrbray/a0ae8ec59d1fd1ae03e2947368096d2e修改后的find . -name 'HEAD'完成

提供了一种仅通过文件夹或文件名搜索gits的替代方法。

[没有足够的声誉来评论Dee的回答,但是我使用了他的解决方案,并且对我有用。

相关问题