Git pull for many repository directories

We have a projects directory that contains many git repository directories:
We want to do git pull in each of these repository directories.

Solution 1: one by one

A novice solution is to change into each directory one by one like this:
  cd ~me/projects/foo && git pull
  cd ~me/projects/goo && git pull  
  cd ~me/projects/hoo && git pull  

Solution 2: find | while loop

A better solution is to use the shell's find command and pipe the results to a simple loop like this:
  find ~me/projects/ \
  | while read line; do cd $line && git pull; done

Solution 3: find | xargs dirname | while loop

To find repository directories and filter out non-repostitory directories, we can use find with options, pipe to the xargs command and dirname command, then pipe to the while loop:

  find ~me/projects/ -type d -name .git \
  | xargs -n 1 dirname \
  | while read line; do cd $line && git pull; done

See Git: How to find git repository directories

Bonus: show progress with "sort" and "echo"

To show progress as the loop runs, we can use the sort command and echo command in the loop like this:
  find ~me/projects/ -type d -name .git \
  | xargs -n 1 dirname \
  | sort \
  | while read line; do echo $line && cd $line && git pull; done

What's Next?

blog comments powered by Disqus