Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git Powershell-用于运行多个后台命令的_Git_Powershell - Fatal编程技术网

Git Powershell-用于运行多个后台命令的

Git 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

我有一个带有10
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的帮助