Last active
June 1, 2025 14:23
-
-
Save griffeth-barker/4d98a393608b03b11d350f247277f76d to your computer and use it in GitHub Desktop.
Pull and rebase updates for all Git repositories in a base directory. This can optionally be scheduled to run via a cron job or Windows Task Scheduler.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Update-GitRepositories { | |
<# | |
.SYNOPSIS | |
Pulls updates for Git repositories. | |
.DESCRIPTION | |
This function invokes `git pull --rebase` on all Git repositories found in the specified base directory. | |
.PARAMETER BaseDirectory | |
The base directory where Git repositories are located. The function will search for directories containing a `.git` subdirectory. | |
Mandatory : true | |
Type : string | |
Position : named | |
.PARAMETER Rebase | |
Pull with rebase. | |
Mandatory : false | |
Type : switch | |
Position : named | |
.EXAMPLE | |
# Pull updates for all Git repositories in the default base directory | |
Update-GitRepositories -BaseDirectory ~/GitHub/ | |
.EXAMPLE | |
# Pull and rebase updates for all Git repositories in the default base directory | |
Update-GitRepositories -Rebase -BaseDirectory ~/GitHub/ | |
.NOTES | |
Requirements: | |
- Operating System(s): | |
- Windows | |
- Linux | |
- macOS | |
- Packages: | |
- "git" | |
This can be scheduled using cron on *nix systems or Task Scheduler on Windows systems. | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$BaseDirectory, | |
[Parameter(Mandatory = $false)] | |
[switch]$Rebase | |
) | |
begin { | |
$repositories = Get-ChildItem -Path $BaseDirectory.Trim() -Directory | Where-Object { Test-Path "$($_.FullName)\.git" } | |
} | |
process { | |
foreach ($repository in $repositories) { | |
Set-Location -Path "$($repository.FullName)" | |
if ($Rebase) { | |
Write-Information "Pulling with rebase: $($repository.FullName)" -InformationAction Continue | |
git pull --rebase | |
} else { | |
Write-Information "Pulling without rebase: $($repository.FullName)" -InformationAction Continue | |
git pull | |
} | |
} | |
} | |
end { | |
Set-Location -Path ~ | |
} | |
} | |
# Optional: Uncomment the line below to run the function immediately. | |
# This was functionalized for portability and reuseability. | |
# Update-GitRepositories -Rebase -BaseDirectory ~/GitHub/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment