Updating all repos in a folder?
Asked Answered
P

10

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.

Peg answered 28/3, 2013 at 23:50 Comment(1)
Is this a duplicate to #3497623?Progress
V
35

Try this:

cd repos
find . -maxdepth 1 -type d -exec sh -c '(cd {} && git pull)' ';'
Vowel answered 28/3, 2013 at 23:53 Comment(5)
Caveat (perhaps an obvious one): This works on *nix or GitBash (and perhaps Mac?), but not the Windows command-line.Pillory
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?Peg
@RuslanOsipov The error that cannyboy mentioned still exists. Did you update your original answer?Lysine
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.Asiatic
how to ignore the . (the current directory)?Mixer
A
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
)
Anyhow answered 25/6, 2018 at 4:50 Comment(0)
S
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.

Siloum answered 9/5, 2016 at 15:19 Comment(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% insteardWeariless
S
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.

Spectacle answered 12/5, 2021 at 7:53 Comment(2)
Why the second "cd ../"?Waiter
The second cd ../ is just for verbosity and can also be removed.Spectacle
S
3

gitfox is a tool to execute command on all subrepo

npm install gitfox -g

g pull

Sculptress answered 13/1, 2017 at 7:1 Comment(0)
T
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.

Thorax answered 28/3, 2013 at 23:56 Comment(2)
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.Vowel
You are correct. I'm missing out the git submodule foreach git pull command.Thorax
G
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.)

Ginsberg answered 30/3, 2013 at 3:5 Comment(0)
A
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

Anzus answered 1/6, 2022 at 21:48 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewFewer
C
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.

Chug answered 26/4, 2023 at 1:52 Comment(0)
P
0

This is an old question, but i would like to suggest using myrepos.

You have a lot of version control repositories. Sometimes you want to update them all at once. Or push out all your local changes. Myrepos provides a mr command, which is a tool to manage all your version control repositories.

cd repos
find . -type d -depth 1 -exec mr register {} \
mr update
Pentachlorophenol answered 1/11, 2023 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.