271

How can I update multiple git repositories from their shared parent's directory without cd'ing into each repo's root directory? I have the following which are all separate git repositories (not submodules):

/plugins/cms
/plugins/admin
/plugins/chart

I want to update them all at once or at least simplify my current workflow:

cd ~/plugins/admin
git pull origin master
cd ../chart
git pull

etc.

Alastair
  • 6,451
  • 3
  • 33
  • 29
Petah
  • 42,792
  • 26
  • 149
  • 203

16 Answers16

360

Run the following from the parent directory, plugins in this case:

find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull origin master \;

To clarify:

  • find . searches the current directory
  • -type d to find directories, not files
  • -depth 1 for a maximum depth of one sub-directory
  • -exec {} \; runs a custom command for every find
  • git --git-dir={}/.git --work-tree=$PWD/{} pull git pulls the individual directories

To play around with find, I recommend using echo after -exec to preview, e.g.:

find . -type d -depth 1 -exec echo git --git-dir={}/.git --work-tree=$PWD/{} status \;

Note: if the -depth 1 option is not available, try -mindepth 1 -maxdepth 1.

leo
  • 6,730
  • 1
  • 22
  • 25
  • Thanks @batandwa for your remark about the use of -d. – leo Jan 26 '13 at 22:02
  • 17
    find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments. – Yuri Astrakhan May 01 '13 at 06:33
  • 32
    I used `find . -maxdepth 1 -type d -print -execdir git --git-dir={}/.git --work-tree=$PWD/{} pull origin master \;` to output the name of the folder before doing the pull, to get rid of the warning and to only run the pull on subfolders. – Rystraum May 15 '13 at 06:36
  • 4
    replacing 'pull origin master' with `fetch origin master:master` tells git to explicitly update your 'master' branch with origin's master branch. This **will not do a merge**, any commits to master will be lost if you do this. – ThorSummoner Jun 24 '14 at 22:18
  • 84
    since git 1.8.5 it is possible to replace --git-dir and --work-tree by the -C option, see [this question](http://stackoverflow.com/questions/5083224/git-pull-while-not-in-a-git-directory). -- I'm using `find . -mindepth 1 -maxdepth 1 -type d -print -exec git -C {} pull \;` – Zarat Mar 11 '15 at 11:06
  • Didnt´t work unfortunately: "find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments. find: paths must precede expression: 1 Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression] " – Zsolt Szilagyi Sep 14 '16 at 14:44
  • 3
    @ZsoltSzilagy as mentioned by @Rystraum you can use `-maxdepth 1` instead of `-depth 1` – jamiebarrow Dec 09 '16 at 07:41
  • Note that when using this solution for bare repositories you have to omit --work-tree and leave only --git-dir, i.e. `find . -type d -depth 1 -exec echo git --git-dir={}/ remote update \;` – mdziob Jan 09 '17 at 15:03
  • I use this to avoid hidden folders `find . -not -path '*/\.*' -mindepth 1 -maxdepth 1 -type d -print -exec git -C {} pull \;` – alayor Jun 18 '18 at 01:39
  • Can we make each `git pull` run in parallel? – Bruno Finger Sep 20 '18 at 08:00
  • Also all the spaces in this line are sensitive I found out. – Alper Sep 27 '18 at 08:44
  • After following some of the comments that explain that depth has changed to maxdepth, I get an error 'missing argument to -exec'. – user1445967 May 02 '19 at 19:44
  • Thank you for this answer, this is what I came up with: find . -type d -name ".git" \ -a -exec echo {} \; \ -exec git --git-dir={} --work-tree=$PWD/{}/../ status \; \ -exec git --git-dir={} --work-tree=$PWD/{}/../ pull \; \ -exec git --git-dir={} --work-tree=$PWD/{}/../ fetch -ap \; – 4F2E4A2E Feb 17 '20 at 10:05
  • depending on how you use, you might add --rebase: `find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull --rebase origin master \;` – srikanth Nutigattu Nov 22 '20 at 15:48
226
ls | xargs -I{} git -C {} pull

To do it in parallel:

ls | xargs -P10 -I{} git -C {} pull
Dmitry Mitskevich
  • 4,218
  • 2
  • 13
  • 8
  • 51
    Nice! I've put it as an alias in my .gitconfig: `all = "!f() { ls | xargs -I{} git -C {} $1; }; f"` Now I can do `git all pull`, `git all "checkout master"` etc. – borisdiakur Jul 13 '15 at 08:39
  • 7
    Cleaned up a bit, will search all directories recursively for only git repos, and will strip out colors in case you have ls aliased `ls -R --directory --color=never */.git | sed 's/\/.git//' | xargs -P10 -I{} git -C {} pull` – cchamberlain Jul 15 '15 at 04:18
  • Here's the git config command to add it as an alias: git config --global alias.all '!f() { ls | xargs -I{} git -C {} $1; }; f' – simonbogarde May 10 '16 at 10:27
  • 8
    I smashed some of the answers together to create this for git on macOS that filters on folder that contain a .git folder, and lets you run arbitrary commands like `git all fetch --prune`: `git config --global alias.all '!f() { ls -R -d */.git | sed 's,\/.git,,' | xargs -P10 -I{} git -C {} $1; }; f'` – Courtney Faulkner Dec 21 '16 at 23:10
  • @CourtneyFaulkner This looks like it assumes a .git directory is available where the command is run. Am I right? Seems different from the original question where just the subdirectories contain .git dirs. – AWrightIV Dec 22 '16 at 17:14
  • 2
    @AWrightIV actually, `ls -R -d */.git` is returning a filtered list of the directories within the current folder that contain a `.git` directory. That way, when I run something like `git all fetch`, it only executes against subfolders that have .git folders. It's an answer to the original question, but it tries to be a bit more efficient by not assuming all the subdirectories are git repos. – Courtney Faulkner Jan 03 '17 at 17:32
  • 11
    A slight improvement over borisdiakur command, to avoid running on `.` and to have a printed list of on which directory it's running at each instant: `git config --global alias.all '!f() { ls -R -d */.git | xargs -I{} bash -c "echo {} && git -C {}/../ $1"; }; f'` – Jose Luis Blanco Dec 06 '17 at 07:51
  • @borisdiakur Is there a trick to run the pull in parallel in all directories ? – Sankar Jul 17 '18 at 04:36
  • I'm getting xargs: unknown option -- C – David S. Feb 04 '21 at 12:19
  • 1
    If you want to avoid non git directories it will be ```bash ls -R -d */.git | cut -d'.' -f1 | xargs -I{} git -C {} pull ``` – Joel Deleep Mar 23 '21 at 09:07
81

A bit more low-tech than leo's solution:

for i in */.git; do ( echo $i; cd $i/..; git pull; ); done

This will update all Git repositories in your working directory. No need to explicitly list their names ("cms", "admin", "chart"). The "cd" command only affects a subshell (spawned using the parenthesis).

Ingo Blechschmidt
  • 1,021
  • 8
  • 9
29

Actually, if you don't know if the subfolders have a git repo or not, the best would be to let find get the repos for you:

find . -type d -name .git -exec git --git-dir={} --work-tree=$PWD/{}/.. pull origin master \;

The PowerShell equivalent would be:

Get-ChildItem -Recurse -Directory -Hidden  -Filter .git | ForEach-Object { & git --git-dir="$($_.FullName)" --work-tree="$(Split-Path $_.FullName -Parent)" pull origin master }
Gildas
  • 440
  • 4
  • 5
27

I use this one:

find . -name ".git" -type d | sed 's/\/.git//' |  xargs -P10 -I{} git -C {} pull

Universal: Updates all git repositories that are below current directory.

Liudvikas
  • 326
  • 3
  • 4
  • 3
    you can use `--git-dir` instead of `-C` to pass in the `.git` path directly removing the need for the sed replacement! ```find . -name ".git" -type d | xargs -P10 -I{} git --git-dir={} pull``` – Chris Jan 03 '19 at 00:45
12

This should happen automatically, so long as cms, admin and chart are all parts of the repository.

A likely issue is that each of these plugins is a git submodule.

Run git help submodule for more information.

EDIT

For doing this in bash:

cd plugins
for f in cms admin chart
do 
  cd $f && git pull origin master && cd ..
done
Jamie Wong
  • 17,388
  • 7
  • 57
  • 81
  • No, sorry you misunderstood. Each of those directories are a separate git repository. `/plugins` is not a repository – Petah Aug 16 '10 at 21:06
  • Ahhh. My mistake. Will give you the bash solution in a minute. – Jamie Wong Aug 16 '10 at 21:44
  • There you go. If you want to return to the parent directory, just run another `cd ..` afterwards. – Jamie Wong Aug 16 '10 at 21:47
  • Or use `pushd` and `popd` or put the group of commands in a subshell (when the subshell exits, you'll be left in the original directory). `(cd dir; for ... done)` – Dennis Williamson Aug 16 '10 at 22:13
  • Cool, that works. Now is there a way to pass the ssh password to the bash script and have the script pass it to git/ssh instead of prompting every time for my password? – Petah Aug 16 '10 at 23:10
  • 5
    Out of curiousity - why are aren't you using ssh keys instead? – Jamie Wong Aug 17 '10 at 03:56
11

The mr utility (a.k.a., myrepos) provides an outstanding solution to this very problem. Install it using your favorite package manager, or just grab the mr script directly from github and put it in $HOME/bin or somewhere else on your PATH. Then, cd to the parent plugins folder shared by these repos and create a basic .mrconfig file with contents similar to the following (adjusting the URLs as needed):

# File: .mrconfig
[cms]
checkout = git clone 'https://<username>@github.com/<username>/cms' 'cms'

[admin]
checkout = git clone 'https://<username>@github.com/<username>/admin' 'admin'

[chart]
checkout = git clone 'https://<username>@github.com/<username>/chart' 'chart'

After that, you can run mr up from the top level plugins folder to pull updates from each repository. (Note that this will also do the initial clone if the target working copy doesn't yet exist.) Other commands you can execute include mr st, mr push, mr log, mr diff, etc—run mr help to see what's possible. There's a mr run command that acts as a pass-through, allowing you to access VCS commands not directly suported by mr itself (e.g., mr run git tag STAGING_081220015). And you can even create your own custom commands that execute arbitrary bits of shell script targeting all repos!

mr is an extremely useful tool for dealing with multiple repos. Since the plugins folder is in your home directory, you might also be interested in vcsh. Together with mr, it provides a powerful mechanism for managing all of your configuration files. See this blog post by Thomas Ferris Nicolaisen for an overview.

evadeflow
  • 4,124
  • 31
  • 45
11

Most compact method, assuming all sub-dirs are git repos:

ls | parallel git -C {} pull
cmcginty
  • 101,562
  • 37
  • 148
  • 155
9

None of the top 5 answers worked for me, and the question talked about directories.

This worked:

for d in *; do pushd $d && git pull && popd; done
phuclv
  • 27,258
  • 11
  • 104
  • 360
Ryan
  • 101
  • 1
  • 3
  • 1
    For Windows, see my answer here: https://stackoverflow.com/a/51016478/207661. It's very similar to above. – Shital Shah Jun 25 '18 at 04:50
  • The accepted answer used to work for me, but it quit sometime in the last year. I finally decided to look for another solution and found your answer. Thanks for sharing. It works perfectly. – BarryMode Jun 08 '20 at 16:18
  • no need to push and pup, just use git's `-C` option. – Travis Stevens Mar 11 '21 at 05:02
7

My humble construction that

  • shows the current path (using python, convenient and just works, see How to get full path of a file?)
  • looks directly for .git subfolder: low chance to emit a git command in a non-git subfolder
  • gets rid of some warnings of find

as follow:

find . \
    -maxdepth 2 -type d \
    -name ".git" \
    -execdir python -c 'import os; print(os.path.abspath("."))' \; \
    -execdir git pull \;

Of course, you may add other git commands with additional -execdir options to find, displaying the branch for instance:

find . \
    -maxdepth 2 -type d \
    -name ".git" \
    -execdir python -c 'import os; print(os.path.abspath("."))' \; \
    -execdir git branch \;
    -execdir git pull \;
Raffi
  • 2,440
  • 24
  • 27
  • 1
    Not sure why there's net downvotes on this answer. This was the most helpful, imo, since I could go to a greater max depth and not keep hitting non-git repos. I used this to run git gc on all all the repositories in my "developer" repo. – Harshita Gupta Mar 20 '19 at 20:50
  • 1
    I also like this answer as it's easy to read and very easy to add multiple commands. – Top Cat Sep 13 '19 at 11:41
5

gitfox is a tool to execute command on all subrepos

npm install gitfox -g
g pull
phuclv
  • 27,258
  • 11
  • 104
  • 360
eqfox
  • 79
  • 1
  • 1
  • what's `g`? not everyone has the same alias as yours – phuclv Apr 27 '17 at 10:00
  • 1
    @LưuVĩnhPhúc gitfox installs itself under the alias "g" for some reason (though the help message says "gitfox"). Personally I do not think it's a command important enough to claim such a shortcut but ah well. It does the job, though. – Stoffe Jul 17 '17 at 09:57
  • @LưuVĩnhPhúc Check the source repo for the usage https://github.com/eqfox/gitfox – thuanle Mar 06 '18 at 02:10
4

You can try this

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

Also, you can add your customized output by adding one more && argument like.

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

I combined points from several comments and answers:

find . -maxdepth 1 -type d -name .git -execdir git pull \;
Nick
  • 3,738
  • 33
  • 53
1

Original answer 2010:

If all of those directories are separate git repo, you should reference them as submodules.

That means your "origin" would be that remote repo 'plugins' which only contains references to subrepos 'cms', 'admin', 'chart'.

A git pull followed by a git submodule update would achieve what your are looking for.


Update January 2016:

With Git 2.8 (Q1 2016), you will be able to fetch submodules in parallel (!) with git fetch --recurse-submodules -j2.
See "How to speed up / parallelize downloads of git submodules using git clone --recursive?"

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • 1
    See also http://stackoverflow.com/questions/1979167/git-submodule-update and http://stackoverflow.com/questions/1030169/git-easy-way-pull-latest-of-all-submodules: `git submodule foreach git pull` can also be of interest. – VonC Aug 16 '10 at 21:31
  • Note: to clarify, '`plugins`', which is not a git repo at the moment, should be made one, as a parent git repo for the submodules. – VonC Aug 16 '10 at 21:33
0

I use this

for dir in $(find . -name ".git")
do cd ${dir%/*}
    echo $PWD
    git pull
    echo ""
    cd - > /dev/null
done

Github

P Pang
  • 234
  • 1
  • 15
-1

If you have a lot of subdirs with git repositories, you can use parallel

ls | parallel -I{} -j100 '
  if [ -d {}/.git ]; then
    echo Pulling {}
    git -C {} pull > /dev/null && echo "pulled" || echo "error :("
  else
     echo {} is not a .git directory
  fi
'
cphyc
  • 401
  • 3
  • 14