Windows for loop through dirs, run git pull?

4

1

From Bash it's simple:

for d in *; do GIT_DIR="$d/.git" git pull; done

Or:

for d in *; do GIT_DIR="$PWD/$d/.git" git pull; done

However from Windows Command Prompt it's not quite as simple. I've tried:

for /D %i in (*.*) do cd "%i" && git pull
for /D %i in (*.*) do cd "<absolute_path>\%i" && git pull
for /D %i in (*.*) do set GIT_DIR="<absolute_path>\%i\.git git pull"
for /D %i in (*.*) do set GIT_DIR="<absolute_path>\%i\.git && git pull"

But none work. Always getting one of these errors:

fatal: unable to access '"repo-name/.git" /config': Invalid argument
The system cannot find the path specified.

A T

Posted 2015-03-16T11:48:10.567

Reputation: 641

are you sure this doesn't work c:\sdf>for /D %i in (*.*) do (CD c:\sdf\%i && DIR && pause) use the pause to troubleshoot. Another variation is c:\sdf>for /D %i in (*.*) do (CD %i && DIR && CD .. && PAUSE) and c:\sdf>for /D %i in (*.*) do (PUSHD %i && DIR && POPD && PAUSE) – barlop – 2015-03-20T10:32:48.197

if you see the FOR is doing what you want, and get it to do what you want, then you can look at the GIT command – barlop – 2015-03-20T10:42:47.537

Answers

5

This works for me in a batch file in CMD:

for /d %%i in (*.*) do cd %%i & git pull & cd..

(Thank you for all great tips I get on this site!)

user708180

Posted 2015-03-16T11:48:10.567

Reputation: 66

Thanks, that worked. Replace %% with % when running purely on CMD (not in a script). – A T – 2017-04-20T06:14:53.680

This works great, thanks – RuSs – 2019-07-25T22:38:26.570

4

Couldn't this be a simple one-liner in Powershell?

Example:

Resolve-Path D:\work\repos\*\.git | foreach { cd $_; git pull }

megamorf

Posted 2015-03-16T11:48:10.567

Reputation: 1 494

I'd prefer to use Windows Batch; is there no way to make the aforementioned for-loop work? – A T – 2015-03-16T20:51:03.130

Powershell is the de facto shell on Windows. Cmd will be deprecated in the future.

There's no use in trying to cobble something together in cmd when there's an object oriented Windows shell right at your fingertips which makes everything so much easier.

-my two cents :-) – megamorf – 2015-03-17T08:19:57.837

1Really? - Well alright, just change your code to D:\work\repos\* and I'll accept. – A T – 2015-03-18T09:18:59.143

The Resolve-Path command as I posted it automatically gets all the folders in repos and then looks for a folder called .git in each of them. That's where you want to cd into according to your code. It's a pretty elegant solution.

If you remove the pipe and everything after it you'll see that it gives you all of the .git folders. – megamorf – 2015-03-18T09:23:16.197

1

Enter powershell into Explorer address field of your base folder and hit enter. Then execute:

Get-ChildItem . -exclude *.ps1,temp,*.txt | foreach { cd $_; Write-Host "`r`n" $_; Git pull '-v' }

Use this approach, if Resolve-Path won't work for you.

Jörg

Posted 2015-03-16T11:48:10.567

Reputation: 707

0

I suggest to use git pull <repository> syntax instead on Git Shell with cmd.exe default shell setting, and look up setlocal enabledelayedexpansion for variable initialization (set) inside for loop.

user373230

Posted 2015-03-16T11:48:10.567

Reputation: