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.

  • It could be clarified whether you want the commands executed recursively (in all subfolders), where mj41's answer is the only one I've tried (from here and at other SO sites) that does that. Nov 8 '19 at 1:48
  • * does that with a tweak that I mention in a comment on it. Nov 8 '19 at 1:54
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
  • If I remove parenthesis it stops working. What's the concept behind it?
    – m0hit
    Dec 17 '19 at 6:28

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

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. Mar 10 '18 at 19:17

This should work

find . -maxdepth 1 -type d -name '.git' -exec sh -c 'cd "{}" && pwd && git pull' \;
  • removing the -maxdepth 1 flag, this is the only command I've found in a lot of searching and trying that executes a command recursively (in all subdirectories, not just the directories one level down). Nov 8 '19 at 1:46

To do it without using find but forloop

for dir in ~/projects/git/*/*/; do (cd "$dir" && git pull); done
  • 1
    That is not recursive, it just shows things that are two levels deep. Nov 2 '20 at 21:31
  • that is correct, thank you. Nov 3 '20 at 8:58

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.