【问题标题】:How to check the status of all git repositories at once?如何一次检查所有 git 存储库的状态?
【发布时间】:2014-08-12 17:06:23
【问题描述】:

简介

为了检查 git 存储库的状态,git status 可以从存储库的根目录发出。

C:\path\to\git_repositories\git_repo_1>git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean

如果一个目录由多个组成,例如50 个 git 仓库

C:\path\to\git_repositories>dir

 Directory of C:\path\to\git_repositories

  .ssh
  git_repo_1
  ...
  git_repo_50
   0 File(s)
   51 Dir(s)

也没有

C:\path\to\git_repositories>git status .
fatal: Not a git repository (or any of the parent directories): .git

都没有

C:\path\to\git_repositories>git status ./.
fatal: Not a git repository (or any of the parent directories): .git

能够检查所有存储库的状态

问题

如何一次查看所有git仓库的状态?

【问题讨论】:

  • 老实说,我更喜欢旧形式的标题,但对于像这样的小事,它可能不值得回去批量编辑。

标签: windows git status


【解决方案1】:

您可以使用 for 循环更改到每个目录,执行 git status 然后更改备份:

for /f "tokens=*" %a in ('dir /ad /b') do cd %a & git status & cd ..

如果在批处理文件中使用,则需要将百分比加倍:

for /f "tokens=*" %%a in ('dir /ad /b') do cd %%a & git status & cd ..

编辑:

按照 Cupcake 的建议,您可以这样做:

for /f "tokens=*" %a in ('dir /ad /b') do git --git-dir=%a/.git --work-tree=%a status

这感觉像是一个更健壮和灵活的解决方案(例如,您可以更轻松地调整它以使用存储在文本文件中的路径列表)。

【讨论】:

  • 将此命令保存在 status.bat fileC:\path\to\git_repositories> 并随后运行它会显示所有 git 存储库的状态。谢谢。
  • @utrecht 一个更好的解决方案(而不是 cd 进入每个文件夹)是简单地告诉 Git 每个存储库在哪里,而不是使用 --git-dir=<path>,例如 git status --git-dir=<path>。请参阅official Git manual for details
  • 实际上,我不确定哪个解决方案会更快,也许cd 实际上会更快。
  • 我搞砸了,如果你打算这样使用--git-dir,那么你还需要用--work-tree=<path>告诉Git每个存储库的工作副本目录在哪里,例如:@ 987654335@。否则,Git 会认为当前工作目录是工作副本,所以你会得到不正确的结果。
  • @Cupcake 将 status.bat 的内容更改为 for /f "tokens=*" %%a in ('dir /ad /b') do git --git-dir=%%a/.git --work-tree=%%a status 也可以。谢谢。
【解决方案2】:

我去了:

find . -name .git -execdir bash -c 'echo -en "\033[1;31m"repo: "\033[1;34m"; basename "`git rev-parse --show-toplevel`"; git status -s' \;

我认为它更好,因为递归地处理目录。

编辑后以更简洁的方式显示 repo 的名称。

【讨论】:

  • 只是检查,这不会获取,对吗?所以如果遥控器上有新东西,那么这不会抓住它?
  • 同样重要的是要注意,使用 -s 标志 git status 将不会显示,例如,您是否落后于远程 n 提交(对我来说是一个主要用例)。我的修改版本如下: find . -name .git -execdir bash -c 'echo -en "\033[1;31m"repo: "\033[1;34m"; basename "git rev-parse --show-toplevel";git fetch;混帐状态 -s -b' \;在这一点上我唯一的问题是 -b 标志即使是最新的也会显示信息。有了这个固定,我认为这是完美的。
【解决方案3】:

如果你是一个工具人,你可以使用一个叫做RepoZ的小帮手@我最近写的(现在还在写)。

我回复了quite similar question in more detail here

这是当前开发阶段的屏幕截图,希望您能看到它是否对您有帮助:

如果您想知道+45 ~403 -88 之类的字符串是什么意思 - 它们是压缩状态字符串,告诉您是否有要获取或推送的提交以及本地是否有添加/修改/删除的文件。更多详情请关注project site on GitHub

【讨论】:

  • 很棒的工具,一致且易于使用。谢谢。
【解决方案4】:

如果您的系统由多个 Git 存储库组成,则可能是针对这种情况创建了 Google repo 工具:https://code.google.com/archive/p/git-repo/

如果您发现自己正在编写脚本以在这些存储库上分发 Git 命令,您可能应该切换到 repo

repo 使用名为“manifest.xml”的(不幸的是)XML 文件,该文件指定工作区中的 Git 存储库。对于每个 repo 指定应该出现哪个分支和提交;您可以使用 repo 将 Git 存储库“固定”到特定版本,或跟踪磁头。

如果您有repo 工作区,那么

$ repo status

然后repo 将完成您所要求的工作:遍历 Git 存储库并为您提供状态。

【讨论】:

    【解决方案5】:

    在 bash 中我发现了这个非常好的命令(感谢 https://coderwall.com/wkjagt):

    find . -maxdepth 1 -mindepth 1 -type d -exec sh -c '(echo {} && cd {} && git status -s && echo)' \;
    

    要在您的 bash_profile 中创建别名,请使用以下转义以使其工作:

    alias gs_recursive='find . -maxdepth 1 -mindepth 1 -type d -exec sh -c "echo {}; cd {}; git status -s; echo" \;'
    

    【讨论】:

      【解决方案6】:

      截至2.34 the for-each-repo command 允许跨多值配置中定义的存储库列表运行 git 命令。

      示例配置:

      [myRepos]
        repo = /full/path/without/expansion/repo1
        repo = ../can/be/relative/repo2
      [alias]
        all = for-each-repo --config=myRepos.repo
      

      然后你可以运行git all status

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-07
        • 2016-01-09
        • 1970-01-01
        • 1970-01-01
        • 2012-01-14
        • 2018-09-11
        • 2016-04-29
        相关资源
        最近更新 更多