简介
为了检查 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仓库的状态?
您可以使用
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
这感觉像是一个更强大、更灵活的解决方案(例如,您可以更轻松地调整它以使用存储在文本文件中的路径列表)。
我去了:
find . -name .git -execdir bash -c 'echo -en "\033[1;31m"repo: "\033[1;34m"; basename "`git rev-parse --show-toplevel`"; git status -s' \;
我认为它更好,因为递归地处理目录。
进行编辑,以更简洁的方式显示存储库的名称。
借自 coderwall - 文件夹中所有存储库的 git status :
find . -maxdepth 1 -mindepth 1 -type d -exec sh -c '(echo {} && cd {} && git status -s && echo)' \;
:查找当前文件夹中的所有内容find .
:这样它就不会递归到存储库的子目录中-maxdepth 1
:跳过当前目录(深度为0)-mindepth 1
:仅查找目录-type d
:生成一个 shell 并给它一个命令-exec sh -c
:给 shell 的命令'(echo {} && cd {} && git status && echo)'
- 找到的目录
:回显echo {}
find
- 找到的目录
: cd 进入cd {}
find
:运行实际的 git status,使用git status -s
(短)选项-s
:回显空行,以提高可读性echo
:分号为每个找到的目录运行 shell,而不是将它们作为参数全部传递给一个 shell\;
此外,您可以在 bash_profile 中创建一个别名,使用以下转义使其工作:
alias gs_recursive='find . -maxdepth 1 -mindepth 1 -type d -exec sh -c "echo {}; cd {}; git status -s; echo" \;'
从 2.34 开始,
for-each-repo
命令 允许在多值配置中定义的存储库列表中运行 git 命令。
示例配置:
[myRepos]
repo = /full/path/without/expansion/repo1
repo = ../can/be/relative/repo2
[alias]
all = for-each-repo --config=myRepos.repo
然后你就可以运行
git all status
。
如果您的系统由众多 Git 存储库组成,则可能是为此创建了 Google
repo
工具的情况:https://code.google.com/archive/p/git-repo/
如果您发现自己正在编写脚本来在这些存储库上分发 Git 命令,您可能应该切换到
repo
。
repo
使用一个(不幸的是)名为“manifest.xml”的 XML 文件,该文件指定工作区中的 Git 存储库。对于每个存储库指定应出现哪个分支和提交;您可以使用 repo
将 Git 存储库“固定”到特定版本,或跟踪头。
如果您有
repo
工作空间,那么
$ repo status
然后
repo
将会完成您所要求的任务:迭代 Git 存储库并为您提供状态。
当我查找这个问题时,我想起了所有使用
cd
跳转到存储库,然后在其中运行 git 命令的示例。
您需要使用选项
--git-dir=
和 --work-tree=
(嗯……这就是我想说的。)
当我们使用这些选项时,
--git-dir
是存储库中的.git
目录,--work-tree=
值是存储库的根目录。
假设我们在
C:\
,我们感兴趣的存储库位于 C:\workspace\foobar
无需进行野蛮的
cd
操作即可获得其状态:
git --git-dir=c:\workspace\foobar\.git --work-tree=c:\workspace\foobar\ status
或者我们都喜欢的美好的短暂状态。
git --git-dir=c:\workspace\foobar\.git --work-tree=c:\workspace\foobar\ status -s
所以这只是打字量的两倍多一点,我们可以使用一些几乎不连贯的 git 选项。
更重要的是,我们还没有降低自己的水平,几乎一起敲打石头并打字......
cd c:\workspace\foobar; git status -s
我们为什么要这么做。
(说真的,做
cd
。很少能找到一个脚本做--git-dir/--work-tree
废话。这是非常糟糕的用户服务,并且具有 0% 设计思想的所有特征。)
现在让我们重新记录...(好吧,我们不这样做。)