20

I've got a folder - 'Repos' - which contains the repos that I'm interested in. These have been cloned from bitbucket, but I guess github could be a source too.

Repos
    - music-app
    - elephant-site
    - node-demo

Is there a git command that I can use which steps thru every folder in Repos and sees if there are new commits on the server, and the download the new stuff? And if there are new commits locally, then upload these.

cannyboy
  • 24,180
  • 40
  • 146
  • 252
  • 1
    Is this a duplicate to http://stackoverflow.com/questions/3497123/run-git-pull-over-all-subdirectories? – Dirk Nov 21 '15 at 10:54

9 Answers9

35

Try this:

cd repos
find . -maxdepth 1 -type d -exec sh -c '(cd {} && git pull)' ';'
Ruslan Osipov
  • 5,655
  • 4
  • 29
  • 44
  • 1
    Caveat (perhaps an obvious one): This works on *nix or GitBash (and perhaps Mac?), but not the Windows command-line. – Code-Apprentice Mar 28 '13 at 23:56
  • i should have mentioned i'm on a mac. ls foreach 'git pull' just returns "fatal: Not a git repository (or any of the parent directories): .git" ... does my Repos folder need to be git repo too? – cannyboy Mar 29 '13 at 00:46
  • @RuslanOsipov The error that cannyboy mentioned still exists. Did you update your original answer? – vm0112 Jun 02 '17 at 18:52
  • I would change this to: `find . -maxdepth 1 -type d -exec sh -c '(cd {} && git checkout master && git pull)' ';'` in case any of those repos aren't already on the master branch. – nnyby Jun 06 '17 at 18:59
  • how to ignore the `.` (the current directory)? – Andy Aldo Sep 10 '17 at 03:08
8

For Windows, I'm using below in .cmd file. It can easily be extended to do something more or something else:

for /d %%i in (*) do (
  pushd "%%i"
  git pull
  popd
)
Shital Shah
  • 63,284
  • 17
  • 238
  • 185
6

For Windows, this should do it for you via a Command Prompt:

cd c:\repos
for /d %a in (*.*) do cd c:\repos\%a && git pull

Once you go back to the GitHub client, you should see the updates.

  • 1
    `for /d %a in (*.*) do cd %CD%\%a && git pull` this one should work as well just no need to specify a spacific path using `%CD%` insteard – angularrocks.com Jan 03 '17 at 04:21
5

With powershell you can do it like that:

Get-ChildItem -Recurse -Depth 2 -Force | 
Where-Object { $_.Mode -match "h" -and $_.FullName -like "*\.git" } |
ForEach-Object {
  cd $_.FullName
  cd ../
  git pull
  cd ../
}

This will go into each directory and look for a .git folder and just run git pull in each of these folders.

S.Spieker
  • 7,005
  • 8
  • 44
  • 50
3

gitfox is a tool to execute command on all subrepo

npm install gitfox -g

g pull

eqfox
  • 97
  • 1
  • 1
0

To maintain several repos, you can use git submodule.

Use git submodule add to add a repo as a submodule and use git submodule foreach git pull to update the repos.

This method is like you have a super project, with several git projects in it.

wmfairuz
  • 1,033
  • 10
  • 19
  • This is **not** how git submodules work. Submodule update will pull certain commit from the histor. You will still have to do `git submodule foreach git pull` in order to update them. – Ruslan Osipov Mar 28 '13 at 23:58
  • You are correct. I'm missing out the git submodule foreach git pull command. – wmfairuz Mar 29 '13 at 00:00
0

What I have is a scriptlet that does something like:

for d in *
cd $d
git pull
cd ..

(Yes, it has a few extra bells and whistles, in that I have a few hg repos, and others I manage in git from SVN upstream.)

vonbrand
  • 11,412
  • 8
  • 32
  • 52
0
  1. cd into the parent directory of the repositories you would like to update
  2. Then run the following
find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull origin \;

credits to Leo

morristech
  • 43
  • 2
  • 7
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31920436) – ldrg Jun 06 '22 at 13:48
0

I don't see any fix for this in the given answers, but a great way to modify the selected answer in order to avoid pulling on "./" repo is to also set the mindepth value on find. The following expression solves the OP question while also removing the error experienced by the selected answer:

cd repos
find . -mindepth 1 -maxdepth 1 -type d -exec sh -c '(cd {} && git pull)' ';'

This works for most *nix systems and should work for mac.

Will Fox
  • 17
  • 1
  • 8