1

I have a directory with 10 git repos. I would like to do git pull on all of them, in parallel, in a single command:

gpa // git-pull-all

This should actually do the following:

cd c:\repos;
foreach $dir in `ls -d` do
    git pull & // unix version for background
    cd ..
end

This should be quite simple in bash (unix). In powershell I find it very complicated. How can I do it properly?

1
  • PS C:\> help about_Jobs Commented Feb 14, 2017 at 21:14

1 Answer 1

2

This would not be an alias (in PowerShell parlance), it would just be a function or a script.

Mostly you would just find the relevant analogues in PowerShell.

So ls in PowerShell is actually an alias for Get-ChildItem, which in PowerShell v3+ also supports a -Directory parameter to return only directories, so that part almost works right away.

Although you can do a foreach($thing in $things) loop, in this case it would be a bit more natural (PowerShell-ey) to pipe into ForEach-Object, so something like this:

$repos = 'C:\repos'
Get-ChildItem -Path $repos -Directory | ForEach-Object -Process {
    Push-Location -Path $_
    git pull
    Pop-Location
}

For reference, using aliases and alternate syntax to make it look the most like your original, it could be done like this:

cd c:\repos
foreach ($dir in (ls -di)) {
    git pull
    cd ..
}

However I recommend the first one because:

  • it preserves your original path
  • it doesn't use aliases
  • it uses what I feel is a more straightforward way of iterating (in PowerShell)

Neither of these examples handle the backgrounding of the task though. I left that out for the time being because it's not as analogous.

To do that you can use PowerShell Jobs. Either with Start-Job or with Invoke-Command -AsJob.

Have a look at how to use jobs and decide if you want to spend the time to apply it for just 10 repos though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.