Preet Sangha
Preet Sangha

Reputation: 65476

Is there a git or bash way to pull multple git repos other than moving to each directory?

I wrote this (brute force) script bring multiple repos up to date.

for app in $(/bin/ls  -d $@)
do
  cd $app
  pwd
  git pull
  cd ..;
done

Is there a simpler way I can do this please?

Upvotes: 2

Views: 66

Answers (1)

wjandrea
wjandrea

Reputation: 32938

You could at least simplify the script:

  1. Don't parse ls
  2. Use for x instead of for x in "$@"
  3. Use a subshell to avoid cd-ing out

And to avoid bugs:

  1. Quote your args
  2. Use -- on cd to avoid arguments being interpreted as options
  3. Skip the loop if cd fails
for app; do
    (
        cd -- "$app" || continue
        pwd
        git pull
    )
done

BTW Shellchek is a great resource for debugging shell scripts. I got most of these tips there.

Upvotes: 4

Related Questions