Parsing the output of “git submodule foreach 'git status'”

I have a Git super project with many submodules. I’d like to be able to go through the submodules and run git status so that I can quickly see which branch each submodule is on and whether there are uncommitted changes. However when I run git submodule foreach 'git status' it is difficult to parse and I can’t tell which submodule it’s reporting the status for. It appears to just spew out my %PATH% variable (I’m using msysGit on Windows). My output appears similar to that shown here:

Entering 'C:\Program Files\Git/libexec/git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\bin;c:\Documents and Settings\username\My Documents\LinuxHome\bin;.;C:\Program Files\Git\local\bin;C:\Program Files\Git\mingw\bin;C:\Program Files\Git\bin;c:\WINDOWS\system32;c:\WINDOWS;c:\WINDOWS\System32\Wbem;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\Program Files\Intel\WiFi\bin\;c:\Program Files\Windows Imaging\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files\TortoiseSVN\bin'
# On branch My_Branch
nothing to commit (working directory clean)
Entering 'C:\Program Files\Git/libexec/git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\bin;c:\Documents and Settings\username\My Documents\LinuxHome\bin;.;C:\Program Files\Git\local\bin;C:\Program Files\Git\mingw\bin;C:\Program Files\Git\bin;c:\WINDOWS\system32;c:\WINDOWS;c:\WINDOWS\System32\Wbem;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\Program Files\Intel\WiFi\bin\;c:\Program Files\Windows Imaging\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files\TortoiseSVN\bin'
# On branch My_Branch
# Your branch is ahead of 'FlashDrive/My_Branch' by 10 commits.
#
nothing to commit (working directory clean)
Entering 'C:\Program Files\Git/libexec/git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\bin;c:\Documents and Settings\username\My Documents\LinuxHome\bin;.;C:\Program Files\Git\local\bin;C:\Program Files\Git\mingw\bin;C:\Program Files\Git\bin;c:\WINDOWS\system32;c:\WINDOWS;c:\WINDOWS\System32\Wbem;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\Program Files\Intel\WiFi\bin\;c:\Program Files\Windows Imaging\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files\TortoiseSVN\bin'
# On branch My_Branch
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   MyApp/Win32/Release/MyApp.exe
#   modified:   MyApp/WinCe600 (ARMV4I)/Release/MyApp.exe
#   modified:   MyApp/WinCe600 (ARMV4I)/Release/Startup.exe
#
no changes added to commit (use "git add" and/or "git commit -a")
Entering 'C:\Program Files\Git/libexec/git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\libexec\git-core;C:\Program Files\Git\bin;c:\Documents and Settings\username\My Documents\LinuxHome\bin;.;C:\Program Files\Git\local\bin;C:\Program Files\Git\mingw\bin;C:\Program Files\Git\bin;c:\WINDOWS\system32;c:\WINDOWS;c:\WINDOWS\System32\Wbem;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\WINDOWS\system32\WindowsPowerShell\v1.0;c:\Program Files\Intel\WiFi\bin\;c:\Program Files\Windows Imaging\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files\TortoiseSVN\bin'
# On branch My_Branch
# Your branch is ahead of 'FlashDrive/My_Branch' by 8 commits.
#
nothing to commit (working directory clean)

As you can see, this is quite difficult to glance at (especially when the output is wrapped) and see what the output of ‘git status’. In addition the name of each submodule is not shown so I have no way of knowing which submodule each section of output refers to.

  • Git log - find last use of function
  • Authentication with Jenkins and Git
  • set folder permissions when deploying cakephp app
  • Git: merge all changes from another branch as a single commit
  • In Git is there a way (or tool) to merge by comparing changes, not files?
  • Does .gitignore have an equivalent for version controlled files?
  • Any help is appreciated! Thanks!

  • working with bitbucket and native android development
  • Merge changes when pull breaks
  • git ignores empty folders
  • How to avoid merge-commit hell on GitHub/BitBucket
  • GIT: finding a list of files (e.g. using git ls-files) including submodules
  • Fastest way to download a GitHub project
  • One Solution collect form web for “Parsing the output of “git submodule foreach 'git status'””

    You fail to mention exactly what it is you want shown…

     git submodule foreach -q git ls-files -m 
    

    would go a long way at showing just the modified files, for example. Other options include

     git submodule foreach -q git diff --shortstat --staged # staged
     git submodule foreach -q git diff --shortstat          # unstaged
     git submodule foreach -q git diff --shortstat HEAD     # staged AND unstaged
    

    You could write a silly small script in the following vein:

     #!/bin/sh
     echo "$(basename .) $(git branch)"
    

    To show just what you want using the submodule directory name for easier identification.

    In case you couldn’t work it out from the above ideas: don’t go and try to parse output that wasn’t designed for machine consumption. Git has a very rich set of subcommands that can help you out in a better way.

    Git Baby is a git and github fan, let's start git clone.