简体   繁体   English

如何在机器上的所有 git 存储库上运行命令?

[英]How to run command on all git repositories on a machine?

Based on this question I came up with this command to find out all .git repositories on my machine.基于这个问题,我想出了这个命令来找出我机器上的所有.git存储库。

find / -type d -name .git 2>&-

And it works relatively fast.而且它的工作速度相对较快。 Though I hear my CPU's fan a little, which shows that my machine does some work.虽然我听到了我的 CPU 风扇的声音,这表明我的机器做了一些工作。

Now I want to be able to loop over the results and run git commands on each of them.现在我希望能够遍历结果并对每个结果运行 git 命令。 For example, I want to get their status to see what should be managed.比如我想获取他们的状态,看看应该管理什么。

I tried:我试过了:

find / -type d -name .git 2>&- | xargs git status

But it didn't work:但它没有用:

fatal: not a git repository (or any of the parent directories): .git致命:不是 git 存储库(或任何父目录):.git

I also tried:我也试过:

find / -type d -name .git 2>&- | xargs dirname | xargs git status

Again, the same error.再次,同样的错误。

What command should I use?我应该使用什么命令?

Your issue is simply : suppose your cwd is $HOME , and your repository is in $HOME/my/repo ,您的问题很简单:假设您的 cwd 是$HOME ,并且您的存储库在$HOME/my/repo
git status $HOME/my/repo will not look for a git repository stored in my/repo , it will search for a repository starting from $HOME and going upwards (and it probably won't find one). git status $HOME/my/repo不会查找存储在my/repo的 git 存储库,它会搜索从$HOME开始向上的存储库(并且可能找不到)。


There are several ways to fix this :有几种方法可以解决这个问题:

  • cd to target directory before running git status :在运行git status之前cd到目标目录:

     cd <path/to/repo>; git status
  • name the target .git repository using git -C :使用git -C命名目标.git存储库:

     git -C <path/to/repo> status

To combine your find command with xargs :将您的find命令与xargs结合使用:

  • use dirname to turn /path/to/repo/.git into /path/to/repo ,使用dirname/path/to/repo/.git变成/path/to/repo
  • use xargs -i to allow to place the argument somewhere else than "the last position" :使用xargs -i允许将参数放置在“最后一个位置”以外的其他位置:
find . -name .git | xargs dirname | xargs -i git -C {} status

# or :
find . -name .git | xargs dirname |\
    xargs -i bash -c "echo ===== {}; git -C {} status"

You can also try and use dirk-thomas/vcstool (python-based)您也可以尝试使用dirk-thomas/vcstool (基于 python)

Vcstool is a version control system (VCS) tool, designed to make working with multiple repositories easier. Vcstool 是一个版本控制系统 (VCS) 工具,旨在简化多个存储库的工作。

vcs status /path/to/several/repos /path/to/other/repos /path/to/single/repo

Other approach (shell-based): sascha-andres/devenv其他方法(基于 shell): sascha-andres/devenv

devenv is a tool to manage projects containing multiple git repositories. devenv是一个管理包含多个 git 存储库的项目的工具。 > With a config you can setup all repositories with one command and even work with the local repositories as if they where one ( eg creating branches in all referenced projects) using the shell. > 通过配置,您可以使用一个命令设置所有存储库,甚至可以使用本地存储库,就像使用 shell 一样(例如在所有引用的项目中创建分支)。

I use this:我用这个:

find . \
  -type d \
  -exec sh -c '[ -e "$1/.git" ]' - {} \; \
  -prune \
  -exec git -C {} status \;

It:它:

  • isn't very fast.不是很快。
  • will skip nested repositories (eg submodules).将跳过嵌套的存储库(例如子模块)。
  • works with work trees, but not bare repositories.适用于工作树,但不适用于裸存储库。

Here's an alternative that should work with bare repositories:这是一个适用于裸存储库的替代方法:

find . \
  -type d \
  -exec sh -c 'git -C "$1" rev-parse --git-dir >/dev/null 2>&1' - {} \; \
  -prune \
  -exec git -C {} status \;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM