7

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.
2
  • 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
    Mar 20, 2015 at 10:32
  • 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
    Mar 20, 2015 at 10:42

4 Answers 4

8

This works for me in a batch file in CMD:

for /d %%i in (*.*) do cd %%i & git pull & cd..
1
  • 1
    Thanks, that worked. Replace %% with % when running purely on CMD (not in a script).
    – A T
    Apr 20, 2017 at 6:14
4

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

Example:

Resolve-Path D:\work\repos\*\.git | foreach { cd $_; git pull }
4
  • I'd prefer to use Windows Batch; is there no way to make the aforementioned for-loop work?
    – A T
    Mar 16, 2015 at 20:51
  • 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
    Mar 17, 2015 at 8:19
  • 1
    Really? - Well alright, just change your code to D:\work\repos\* and I'll accept.
    – A T
    Mar 18, 2015 at 9:18
  • 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
    Mar 18, 2015 at 9:23
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.

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.

You must log in to answer this question.

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