Skip to content

Instantly share code, notes, and snippets.

@mehiel
Last active June 25, 2021 07:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehiel/341e7b6a78c6c35dcabc7ca60add8947 to your computer and use it in GitHub Desktop.
Save mehiel/341e7b6a78c6c35dcabc7ca60add8947 to your computer and use it in GitHub Desktop.
Execute git command on all direct git subdirectories of the current directory using `git all`
#!/usr/bin/env bash
git some * -- "$@"
#!/usr/bin/env bash
dir_args_array=()
dir_args_end=0
git_args_array=()
whitespace="[[:space:]]"
for i in "$@"
do
if [ "$i" == "--" ]; then
dir_args_end=1
continue
fi
if [[ $i =~ $whitespace ]]
then
i=\"$i\"
fi
if [ "$dir_args_end" == "0" ]; then
dir_args_array+=("$i")
else
git_args_array+=("$i")
fi
done
git_args=`echo ${git_args_array[@]}`
for d in "${dir_args_array[@]}"; do
[ -d "$d/.git" ] || continue
echo "./$d"
git --no-pager -C "$d" $git_args
done

If you put the git-all command provided in this gist as an executable in your PATH then:

For a structure like:

/just/a/project/group/path

  • project-1/
  • project-2/
  • project-3/

Where project-{1,2,3} are all git directories we can execute the following:

$ cd /just/a/project/group/path  
$ git all status -s  
./project-1  
?? some-new-folder/  
./project-2  
 M src/file.yml  
./project-3  

The empty output after project-3 is obviously no status change reported by git status -s.
Of course all commands work even though some of them may not be appropriate for bulk execution. But you can simultaneously checkout branches, pull, log --oneline and more.

git-some behave the same except it takes specific project to apply the commands to. Example:

$ cd /just/a/project/group/path  
$ git some project-1 project-2 -- status -s  
./project-1  
?? some-new-folder/  
./project-2  
 M src/file.yml 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment