45

I have a set of repositories sorted into directories based on their VCS (Git, Mercurial, SVN). With Subversion I was able to run svn update * in the parent directory and it would loop through each directory and update each repository as expected. That's not the case for Git or Mercurial.

I would like to come up with a bash script that I can run to do exactly that, loop through directories and either git pull or hg pull in each. I just don't have much bash scripting experience.

77
for dir in ~/projects/git/*; do (cd "$dir" && git pull); done
  • 2
    Thnx... but i need to capture errors as well. How can I do so. I've never done shell programming before. – shashwat Feb 16 '15 at 10:37
21

If you need it to be recursive:

find . -type d -name .git -exec sh -c "cd \"{}\"/../ && pwd && git pull" \;

This will descend into all the directories under the current one, and perform a git pull on those subdirectories that have a .git directory (you can limit it with -maxdepth).

  • @thprivileges: clever! – Dennis Williamson Jan 6 '12 at 21:41
  • 8
    -execdir is much better here: find . -type d -name .git -execdir sh -c "pwd && git pull" \; – daniel kullmann Jan 9 '12 at 7:51
  • This is a better answer in my subjective opinion... Worked immediately for me.. Cheers – Voke Ransom Anighoro Jun 10 '15 at 14:30
  • Unlike the question that linked me here, this command actually worked. Thank you. – user1445967 May 2 at 19:47
7

If you have GNU Parallel http:// www.gnu.org/software/parallel/ installed you can do this:

cd ~/projects/git/; ls | parallel 'cd {} && git pull'

This will run in parallel so if some of the git servers' network connection are slow this may speed up things.

Watch the intro video for GNU Parallel to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ

  • Maybe do ls */ to be sure you're giving coherent input to cd. – Joe Corneli Mar 10 '18 at 19:17
3

This should work

find . -maxdepth 1 -type d -name '.git' -exec sh -c 'cd "{}" && pwd && git pull' \;
0

To do it recursive without using find but forloop

for dir in ~/projects/git/*/*/; do (cd "$dir" && git pull); done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.