1

I am using Git Bash, and I would like to write a script that processes the same set of commands for each directory (local repo) in my home directory. This would be easy enough in DOS, which most consider as handicapped at best, so I'm sure there's a way to do it in bash.

For example, some pseudo-code:

ls --directories-in-this-folder -> $repo_list
for each $folder in $repo_list do {
  ...my commmand set for each repo...
}

Does anyone know how to do this in Bash?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jim Fell
  • 13,750
  • 36
  • 127
  • 202

1 Answers1

1

You can do that in bash (even on Windows, if you name your script git-xxx anywhere in your %PATH%)

#! /bin/bash
cd /your/git/repos/dir
for folder in $(ls -1); do
  cd /your/git/repos/dir/$folder
  # your git command
done

As mentioned in "Git Status Across Multiple Repositories on a Mac", you don't even have to cd into the git repo folder in order to execute a git command:

#! /bin/bash
cd /your/git/repos/dir
for folder in $(ls -1); do
  worktree=/your/git/repos/dir/$folder
  gitdir=$worktree/.git # for non-bare repos
  # your git command
  git --git-dir=$gitdir --work-tree=$worktree ...
done
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks, but I'm getting an error: `sh.exe": `repo_dir=$(ls -1)': not a valid identifier`. If I run `ls -1` by itself in the script it works fine, but the error occurs when part of the for statement. – Jim Fell Apr 17 '15 at 20:06
  • @JimFell Sorry: http://www.cyberciti.biz/faq/bash-for-loop/. I will edit the answer. – VonC Apr 17 '15 at 20:10