Skip to content

Instantly share code, notes, and snippets.

@marhensa
Last active August 28, 2023 18:24
Show Gist options
  • Save marhensa/a6ac876912022a6cafbe8df276bcafbb to your computer and use it in GitHub Desktop.
Save marhensa/a6ac876912022a6cafbe8df276bcafbb to your computer and use it in GitHub Desktop.
PowerShell - git pull for all directories (update all repository folders)
Get-ChildItem -Directory -Force -Recurse -Filter ".git" -Depth 1 | ForEach-Object {$currentLocation = Get-Location; Set-Location -Path $_.Parent.FullName; Write-Host $_.Parent.FullName; git pull; Set-Location -Path $currentLocation}
# make sure to run it on the folder that have all those repositories that needs to be updated
@marhensa
Copy link
Author

marhensa commented Aug 28, 2023

to make an alias, so it could be run in a single simple command: gitpullall

$profileContent = @"

function gitpullall {
    Get-ChildItem -Directory -Force -Recurse -Filter ".git" -Depth 1 | ForEach-Object {
        `$currentLocation = Get-Location
        Set-Location -Path `$_.Parent.FullName
        Write-Host `$_.Parent.FullName
        git pull
        Set-Location -Path `$currentLocation
    }
}

"@

$profilePath = $PROFILE

# Check if the function gitpullall already exists in the profile content
if (Get-Content -Path $profilePath | Select-String -Pattern "function gitpullall") {
    $existingContent = Get-Content -Path $profilePath | Out-String
    $newContent = $existingContent -replace "(?s)function gitpullall.*?}.*?}", ""
    Set-Content -Path $profilePath -Value $newContent
}

Add-Content -Path $profilePath -Value "`r`n$profileContent"
.$profilePath

Clear-Host;Write-Host "Now 'gitpullall' is a command to update all repositories folder."


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment