Skip to content

Instantly share code, notes, and snippets.

@dbu
Created May 31, 2012 14:14
Show Gist options
  • Select an option

  • Save dbu/2843660 to your computer and use it in GitHub Desktop.

Select an option

Save dbu/2843660 to your computer and use it in GitHub Desktop.
recursive git status
#!/usr/bin/php
<?php
$repos = array();
exec('find -type d -name .git | sed -e "s/\.git//"', $repos);
foreach ($repos as $repo) {
$status = shell_exec("cd $repo && git status");
if (false == strpos($status, 'nothing to commit (working directory clean)')) {
echo "$repo\n" . str_repeat('-', strlen($repo)) . "\n$status\n\n";
}
}
@igorw

igorw commented May 31, 2012

Copy link
Copy Markdown

Protip: git status -s

@dbu

dbu commented May 31, 2012

Copy link
Copy Markdown
Author

hm, i print the full change log if there is a change. i think its useful to see that to better see what happened.

@nicferrier

Copy link
Copy Markdown

How about:

find . -type d | while read dir ; git status -s $dir ; done

instead?

@Aaron3

Aaron3 commented Jul 20, 2015

Copy link
Copy Markdown

Update to the previous code:

find . -type d | while read dir ; do git status -s $dir ; done

@artembeloglazov

Copy link
Copy Markdown

yet another version:

find . -name '.git' -type d | while read dir ; do sh -c "cd $dir/../ && git status" ; done

@tafkey

tafkey commented Sep 28, 2015

Copy link
Copy Markdown

one more:

find . -type d -name '.git' | while read dir ; do sh -c "cd $dir/../ && echo -e \"\nGIT STATUS IN ${dir//\.git/}\" && git status -s" ; done

@arccoder

Copy link
Copy Markdown

git status -uall

@dooferlad

dooferlad commented Jul 6, 2016

Copy link
Copy Markdown

Output a directory name only if something has changed in that git repo:

find . -type d -name '.git' | while read dir ; do sh -c "cd $dir/../ && git status -s | grep -q [azAZ09] && echo ---- ${dir//\.git/} ---- && git status -s" ; done

@ftiff

ftiff commented Jan 8, 2018

Copy link
Copy Markdown

I use this one:
find . -type d -depth 1 | while read dir ; do echo -e "\n\033[1m${dir}\033[m"; git -C "${dir}" status -s ; done

Key is to use -C $dir or you'll probably get fatal: Not a git repository (or any of the parent directories): .git

From git(1):

-C path
Run as if git was started in instead of the current working directory.

I also use this command to show me which Git has not "origin" remote:
find . -type d -depth 1 | while read dir ; do echo -e "\n\033[1m${dir}\033[m"; git -C "${dir}" remote show origin > /dev/null 2>&1 || echo -e "\033[0;31mNO REMOTE\033[0m"; git -C "${dir}" status -s ; done

Output looks like:

./SplashBuddy

./Send_Message_to_M
NO REMOTE
 M .DS_Store
 M "Send Message to M/Base.lproj/main.xib"

@vinsleo

vinsleo commented Aug 2, 2018

Copy link
Copy Markdown

probably too late. but just adding so that person ending up here has one more choice
find . -name .git -type d -execdir git status -s ';'

@ssshukla26

ssshukla26 commented Aug 9, 2018

Copy link
Copy Markdown
    #! /bin/sh

    BLACK=`tput setaf 0`
    RED=`tput setaf 1`
    GREEN=`tput setaf 2`
    YELLOW=`tput setaf 3`
    BLUE=`tput setaf 4`
    MAGENTA=`tput setaf 5`
    CYAN=`tput setaf 6`
    WHITE=`tput setaf 7`

    BOLD=`tput bold`
    RESET=`tput sgr0`

    find . -name ".git" -type d | while read dir; do echo -e ${YELLOW}${BOLD}${dir/.git/}${RESET}; sh -c "cd ${dir/.git/} && git status"; done

@jtuz

jtuz commented Jun 29, 2019

Copy link
Copy Markdown

git status -uall

The best!

@hoijui

hoijui commented Apr 13, 2020

Copy link
Copy Markdown

based on:

changes:

  • .git is actually often not a directory, but a file (in the case of sub-modules), thus I had to remove the -type d part
  • modified the grep ... part to not exclude the parent repo, if it contains changes

features:

  • fat dir name
  • only show (sub-)repos with changes
  • no sub-shells (for performance)
  • works for non-dir .git sub-modules

one-liner:

find . -name '.git' | while read repo ; do repo=${repo//\.git/}; git -C "$repo" status -s | grep -q -v "^\$" && echo -e "\n\033[1m${repo}\033[m" && git -C "$repo" status -s || true; done

as bash script file with custom extra options for status:

#!/usr/bin/env bash
# Recursive `git status` (including sub-modules)

set -e

status_ops="$*"

find . -name '.git' \
	| while read repo
do
	repo=${repo//\.git/}
	git -C "$repo" status -s \
		| grep -q -v "^\$" \
		&& echo -e "\n\033[1m${repo}\033[m" \
		&& git -C "$repo" status $status_ops \
		|| true
done

If you store this in your PATH as git-status-recursive,
and use git config --global alias.rst '!git-status-recursive' (for example in your ~/.profile),
then you can use it like git rst -s.

@Gabriel-p

Gabriel-p commented Jul 31, 2020

Copy link
Copy Markdown

Thank you @hoijui!

One comment: the script fails if the folder contains '.git' in its name. For example, I have a Github page repo named Gabriel-p.github.io and the script will fail with:

fatal: cannot change to './Gabriel-phub.io/': No such file or directory

@hoijui

hoijui commented Jul 31, 2020

Copy link
Copy Markdown

@Gabriel-p strange.. it does not happen here. :-/

I tested it like this:

mkdir tmp
cd tmp
mkdir .git
mkdir bla.github
mkdir ergwergwerg
mkdir ergwergwerggit
mkdir ergwergwerg/bla.github

find . -name '.git'

Which spits out only this:

./.git

which I think.. is what should happen, according to the man page of find; -name should match only on the complete file name.

Is it possible that you are using some obscure version of find? maybe OSX?
though even there it woudl be strange to diverge like this... I don't know!

@Gabriel-p

Gabriel-p commented Jul 31, 2020

Copy link
Copy Markdown

@hoijui Try creating a .git folder inside bla.github (and remove the top .git folder to avoid an error because it is not a valid git folder), and then run your script. I get this:

fatal: cannot change to './blahub/': No such file or directory

The problem is not with the find . -name '.git' line but with the repo=${repo//\.git/} line. Using this instead

 repo=${repo%".git"}

seems to do the trick.

@hoijui

hoijui commented Sep 7, 2020

Copy link
Copy Markdown

ahh very good, thank you @Gabriel-p!

// replaces anywhere, % replaces only a match on the end of the string, so % is to be used here.

fixed version of the script:

#!/usr/bin/env bash
# Recursive `git status` (including sub-modules)

set -e

status_ops="$*"

find . -name '.git' \
	| while read -r repo
do
	repo=${repo%".git"}
	(git -C "$repo" status -s \
		| grep -q -v "^\$" \
		&& echo -e "\n\033[1m${repo}\033[m" \
		&& git -C "$repo" status $status_ops) \
		|| true
done

... and as one-liner:

find . -name '.git' | while read -r repo ; do repo=${repo%".git"}; (git -C "$repo" status -s | grep -q -v "^\$" && echo -e "\n\033[1m${repo}\033[m" && git -C "$repo" status -s) || true; done

@glittle

glittle commented Feb 7, 2021

Copy link
Copy Markdown

For Windows users... try this in the root folder above the folders to search:

 for /f "tokens=*" %a in ('dir .git /adh /b /s') do (echo off & cd "%~pa" & cd & git status -s)

(The echo off helps the output look less cluttered but may wreck your prompt. If so, exit the window when done.)

@elisherer

Copy link
Copy Markdown

@glittle , awesome,
If you want to check all git folders in "C:\some-folder" run the following (1 level, runs faster)
for /f "tokens=*" %a in ('dir /ad /b') do (echo off & cd "c:\\some-folder\\%~a" & cd & git status -s)

@eduardobcastro

Copy link
Copy Markdown

You don't need to search everything. Do:

find . -maxdepth 2 -name .git -type d -exec sh -c "cd {}/..;git status" \;

@rnsv

rnsv commented Apr 18, 2022

Copy link
Copy Markdown

find . -name '.git' | while read repo; do echo -e "\n$(dirname $repo)"; git --git-dir=$repo --work-tree=$(dirname $repo) status -s -b; done

@franrivasortega

Copy link
Copy Markdown

ahh very good, thank you @Gabriel-p!

// replaces anywhere, % replaces only a match on the end of the string, so % is to be used here.

fixed version of the script:

#!/usr/bin/env bash
# Recursive `git status` (including sub-modules)

set -e

status_ops="$*"

find . -name '.git' \
	| while read -r repo
do
	repo=${repo%".git"}
	(git -C "$repo" status -s \
		| grep -q -v "^\$" \
		&& echo -e "\n\033[1m${repo}\033[m" \
		&& git -C "$repo" status $status_ops) \
		|| true
done

... and as one-liner:

find . -name '.git' | while read -r repo ; do repo=${repo%".git"}; (git -C "$repo" status -s | grep -q -v "^\$" && echo -e "\n\033[1m${repo}\033[m" && git -C "$repo" status -s) || true; done

I discovered from here that if you want to run that script as a git command without making an alias, you have to:

  1. Name your script: "git-command"
  2. Make your script executable
  3. Move your script to a directory that's in your PATH

E.g.:

If your PATH contains ~/.local/bin/ and you named your script "git-str" then:

chmod +x git-str && mv git-str ~/.local/bin/

Now you can run: git str.

To any Linux novice, beware of what script you make executable, as it may be dangerous for your system.

@chengkiang

Copy link
Copy Markdown

(The echo off helps the output look less cluttered but may wreck your prompt. If so, exit the window when done.)

after the command is done, typing echo on will bring back the prompt.

@tafkey

tafkey commented Jul 26, 2023

Copy link
Copy Markdown

This one (1) produces a slim output, and (2) starts printing the results instantly (you don't have to wait it to finish to see the final output):

find . -maxdepth 2 -name .git -type d -exec sh -c "cd {}/..; pwd; git status -s" \;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment