Skip to content

Instantly share code, notes, and snippets.

@dbu
Created May 31, 2012 14:14
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dbu/2843660 to your computer and use it in GitHub Desktop.
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";
}
}
@fran496
Copy link

fran496 commented Apr 8, 2023

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

(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
Copy link

tafkey commented Jul 26, 2023

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