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, 2019 at 1:48
  • * does that with a tweak that I mention in a comment on it. Nov 8, 2019 at 1:54

5 Answers 5

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, 2015 at 10:37
  • If I remove parenthesis it stops working. What's the concept behind it?
    – mohit
    Dec 17, 2019 at 6:28
  • @mohit parentheses create a sub-shell where the commands are executed, so the cd will descend into the given directory and then when the sub-shell exits you'll be back in the original directory. Mar 23, 2022 at 15:41

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

If the dir listing is < 128 KBytes, you can also do:

parallel 'cd {} && git pull' ::: ~/projects/git/*/

If you want to follow the pull happening (requires version 20230322):

parallel --ll 'cd {} && git pull' ::: ~/projects/git/*/

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, 2018 at 19:17
  • Using the same idea, another option could be something like this: find ~/projects/git/ -type d -maxdepth 1 -mindepth 1 -exec realpath {} \; | parallel "cd {} && git pull" ` Apr 21 at 1:47

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, 2019 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, 2020 at 21:31
  • that is correct, thank you. Nov 3, 2020 at 8:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .