【问题标题】:Git Status Across Multiple Repositories on a MacMac 上跨多个存储库的 Git 状态
【发布时间】:2011-02-15 10:53:18
【问题描述】:

我一直在寻找解决这个问题的方法,但还没有找到我需要的东西。

我在我的 Mac (OSX 10.6) 上的一个文件夹中有几个 Git 存储库,我想要一个脚本或工具来循环遍历所有存储库并让我知道它们是否需要 commiting。

这是我的结构

Sites
  /project1
  /project2
  /project3

我希望该工具在 Sites/project1、Sites/project2、Sites/project3 中执行 git status,并让我知道其中是否有任何更改或需要暂存或提交的新文件。

我发现的最接近的脚本可能是可破解的 is here,,但即使该脚本也无法运行,并且我收到错误消息:

“意外标记 `do 附近的语法错误”

可能是为 *nix 编写的。

【问题讨论】:

  • OS X Unix。 ;) 您是否直接复制了脚本?我并不是要成为“那个人”,但它对我有用(即,我在运行它时没有遇到语法错误)。
  • 是的,我知道,但我认为我的 bash 版本缺少一些特殊的 Unix 功能。我刚刚尝试复制“原始”代码并且它现在可以工作 - 所以它可能与我之前从网页复制它有关。感谢您确认它有效。

标签: git bash macos shell


【解决方案1】:

看来这个问题已经很好地回答了,但我想在做同样的事情后投入两分钱。

我通过使用一个简单的 bash 脚本更接近 jcordasc 的答案。我只是做了一件事有点不同。查看 git 的帮助文档,您可以设置 git 目录和工作目录。这消除了“cd”到目录的需要。并不是说它真的有那么大的不同......

#!/bin/bash

for gitdir in `find ./ -name .git`; 
    do 
        workdir=${gitdir%/*}; 
        echo; 
        echo $workdir; 
        git --git-dir=$gitdir --work-tree=$workdir status; 
    done

显然,他的状态显示方式更先进/更清晰......

【讨论】:

  • 老实说,我宁愿将此作为对 jcordasc 答案的评论发布,但该死的,如果我能看到如何做到这一点......
  • 您可以只使用git -C $gitdir 而不是--gitdir--work-tree 选项。所以 do 块的最后一行变成了git -C $gitdir status;
【解决方案2】:

有一个基于 Python 的程序,uncommitted,听起来它完全可以满足您的需求。目前还没有 git 支持(只有 hg 和 Subversion),但你可以帮助作者在他的应用程序中实现 git 支持,或者将他的想法作为如何实现你的东西(他在项目页面上记录了他的查找方法我链接到)。

【讨论】:

  • 谢谢瑞恩!我最终创建了一个支持 Git 的版本,并请求在 bitbucket 上拉取并在 github 上创建了我的存储库 - github.com/eapen/uncommitted
  • os x 用户,要安装: sudo easy_install pip -> sudo pip install uncommitted -> uncommitted ~
  • 主版本现在支持 Git。
【解决方案3】:

这是一个较老的问题,但我去更新了 jcordasc 的答案,因此它适用于 git 1.7.7.5,我想我不妨在这里贡献它:

https://gist.github.com/3018100

此版本将任何路径或路径作为参数,并为它在由参数表示的文件树中的任何位置找到的任何 git 存储库提供 jcordasc 的输出。它还支持检测未推送和未合并的提交。

【讨论】:

    【解决方案4】:

    使用单个 shell 别名检查多个 repo 状态。无需安装。所有致谢:https://coderwall.com/p/grmruq/git-status-on-all-repos-in-folder

    对于 bash:

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

    或用于 Cshell

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

    【讨论】:

      【解决方案5】:

      如果您只想要本地存储库中文件的状态,像这样:http://gist.github.com/389478 应该可以解决问题。您需要修改 for 循环中的模式以获取您想要的任何目录。

      【讨论】:

      • 我很快就会尝试这个,当我第一次尝试使用“原始”模式运行它时,我遇到了相同的语法错误,这可能与换行符有关。一旦我试一试,我会告诉你的。
      【解决方案6】:

      我花了一些时间从@Andrew Roberts 获取代码来处理一组文件夹。如果您也遇到了问题,请在https://gist.github.com/3666392 上查看我的 fork

      感谢 @Andrew Roberts 和 @jcordasc 提供的精彩脚本 .. 正是我所需要的。

      【讨论】:

        【解决方案7】:

        今天我也遇到了同样的问题,除了这个页面之外,我发现它非常有用并且有很好的提示,我还遇到了following blog post。它引入了一个 python 脚本,可以做到这一点:

        Usage: show_status [options]
        
        Show Status is awesome. If you tell it a directory to look in, it'll scan
        through all the sub dirs looking for a .git directory. When it finds one it'll
        look to see if there are any changes and let you know. It can also push and
        pull to/from a remote location (like github.com) (but only if there are no
        changes.) Contact mike@mikepearce.net for any support.
        
        Options:
          -h, --help            show this help message and exit
          -d DIRNAME, --dir=DIRNAME
                                The directory to parse sub dirs from
          -v, --verbose         Show the full detail of git status
          -r REMOTE, --remote=REMOTE
                                Push to the master (remotename:branchname)
          -p PULL, --pull=PULL  Pull from the master (remotename:branchname)
        

        整个东西位于this git repo

        【讨论】:

        • 当我尝试运行它时,我得到inconsistent use of tabs and spaces in indentation
        【解决方案8】:

        聚会很晚,但我写了一个工具来帮我做这件事。这是一个简单的shell脚本,可以找到on Github.

        看起来像这样:

        【讨论】:

          【解决方案9】:

          这是一种在单行中执行此类操作的简单方法。我错过了最后一步,一旦我发现它就会添加(除非其他人知道)。

          对于git status,您可以这样做:

          find ./Sites -name .git | awk '{ print "git --git-dir=" $1 " --work-tree " substr($1,1,length($1) - 4) " status" }'
          

          然后复制输出并执行它。

          对于完整的差异,您可以运行:

          find ./Sites -name .git | awk '{ print "git --git-dir=" $1 " --work-tree " substr($1,1,length($1) - 4) " --no-pager diff" }'
          

          你必须做的复制粘贴事情让我很恼火。它应该在那里也使用xargs,但不知何故,git抱怨:

          find ./Sites -name .git | awk '{ print "--git-dir=" $1 " --work-tree " substr($1,1,length($1) - 4) " --no-pager diff" }' | xargs git
          

          如果有人知道如何在最后一个命令中修复 git 投诉,请编辑此答案。

          【讨论】:

            【解决方案10】:

            还有一个 ruby​​ gem 可以用作 git sub 命令:https://github.com/reednj/git-status-all

            # install the gem    
            gem install git-status-all
            
            # use the status-all subcommand to scan the directory
            git status-all 
            

            【讨论】:

              【解决方案11】:

              @oxtay 的答案是最漂亮的,所以我对其进行了修改以使其更漂亮:https://github.com/Flurrywinde/Git-Status

              我还修复了一些错误并使其即使在一些导致@Ferry Boender 抛出错误的混乱存储库中也能正常工作:

              ./git-ftp: ok 
              ./kphotoalbum: ls: cannot access './kphotoalbum/.git/refs/heads': No such file or directory
              fatal: Not a git repository: './kphotoalbum/.git'
              fatal: Not a git repository: './kphotoalbum/.git'
              fatal: Not a git repository: './kphotoalbum/.git'
              Uncommitted changes 
              ./SemanticForms: Uncommitted changes 
              ./git-big-picture: Uncommitted changes Untracked files 
              ./10-minute-vim-exercises: ok 
              ./moviemasher: Uncommitted changes 
              ./tumbdlwtf: Uncommitted changes 
              ./rotatezoomHTML5video: Uncommitted changes 
              ./tumbdl: Needs push (master) 
              .: ls: cannot access './python/refs/heads': No such file or directory
              fatal: Not a git repository: './python'
              fatal: Not a git repository: './python'
              fatal: Not a git repository: './python'
              Uncommitted changes 
              .: ls: cannot access 'mediawiki/refs/heads': No such file or directory
              fatal: Not a git repository: 'mediawiki'
              fatal: Not a git repository: 'mediawiki'
              fatal: Not a git repository: 'mediawiki'
              Uncommitted changes 
              parser: ls: cannot access 'parser/.git/refs/heads': No such file or directory
              fatal: Not a git repository: 'parser/.git'
              fatal: Not a git repository: 'parser/.git'
              fatal: Not a git repository: 'parser/.git'
              Uncommitted changes 
              ./gundo.orig: Uncommitted changes 
              ./wikiteam: Uncommitted changes Untracked files
              

              @Nathan 的 ruby​​ 似乎有一个错误,当 repo 正常时给出 [ERROR]:

              $ git status-all
              yourls-popular-clicks-extended                                                     [ERROR]
              undefined local variable or method `s' for "yourls-popular-clicks-extended":String
              miniProxy                                                                         [master]
              dmenu-4.6-kanon          M15U37                                                   [master]
              freeCodeCamp                                                                       [ERROR]
              undefined method `up_to_date?' for nil:NilClass
              
              $ mgitstatus                       
              ./yourls-popular-clicks-extended: ok 
              ./miniProxy: ok 
              ./dmenu-4.6-kanon: Uncommitted changes Untracked files 
              ./freeCodeCamp: Untracked files
              

              请注意它对 yourls-popular-clicks-extended 和 freeCodeCamp 的表述为 [ERROR]。 Mine 和 Ferry 做对了。

              如果作为watch 的子进程运行,Mine 也会保留其颜色,这样可以方便地清理你的存储库并实时查看每个存储库。

              Screenshot of mine in a tmux session where I'm cleaning up the repos

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2023-01-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-01-14
                • 1970-01-01
                相关资源
                最近更新 更多