Git Powershell-用于运行多个后台命令的
我有一个带有10Git Powershell-用于运行多个后台命令的,git,powershell,Git,Powershell,我有一个带有10gitrepos的目录。 我想在一个命令中并行地对它们执行git pull: gpa // git-pull-all 这实际上应该做到以下几点: cd c:\repos; foreach $dir in `ls -d` do git pull & // unix version for background cd .. end 这在bash(unix)中应该非常简单。在powershell中,我发现它非常复杂。 如何正确地执行此操作?这不是别名(用Po
git
repos的目录。
我想在一个命令中并行地对它们执行git pull
:
gpa // git-pull-all
这实际上应该做到以下几点:
cd c:\repos;
foreach $dir in `ls -d` do
git pull & // unix version for background
cd ..
end
这在bash
(unix)中应该非常简单。在powershell中,我发现它非常复杂。
如何正确地执行此操作?这不是别名(用PowerShell的说法),它只是一个函数或脚本 大多数情况下,您只能在PowerShell中找到相关的类似物 因此,PowerShell中的
ls
实际上是Get ChildItem
的别名,它在PowerShell v3+中还支持-Directory
参数以仅返回目录,因此该部分几乎可以立即工作
虽然您可以执行foreach($things中的thing)
循环,但在这种情况下,将管道插入foreach对象
会更自然一些(PowerShell ey),因此如下所示:
$repos = 'C:\repos'
Get-ChildItem -Path $repos -Directory | ForEach-Object -Process {
Push-Location -Path $_
git pull
Pop-Location
}
cd c:\repos
foreach ($dir in (ls -di)) {
git pull
cd ..
}
作为参考,使用别名和替代语法使其看起来最像您的原始版本,可以这样做:
$repos = 'C:\repos'
Get-ChildItem -Path $repos -Directory | ForEach-Object -Process {
Push-Location -Path $_
git pull
Pop-Location
}
cd c:\repos
foreach ($dir in (ls -di)) {
git pull
cd ..
}
但是,我推荐第一个,因为:
- 它将保留原始路径
- 它不使用别名
- 它使用了我认为更直接的迭代方式(在PowerShell中)
Start Job
或Invoke命令-AsJob
看看如何使用jobs,然后决定是否要花时间申请10次回购。
PS C:\>关于jobs的帮助