Skip to content

Instantly share code, notes, and snippets.

@simon-reynolds
Last active January 18, 2024 20:38
Show Gist options
  • Save simon-reynolds/04215428efeb813e0308f9a7537a4d55 to your computer and use it in GitHub Desktop.
Save simon-reynolds/04215428efeb813e0308f9a7537a4d55 to your computer and use it in GitHub Desktop.
Powershell script to fetch changes in all repos in directory and pull changes if pristine on default branches
function Get-Changes {
Push-Location ".."
$branch= &git rev-parse --abbrev-ref HEAD
Write-Host "Current branch: " $branch
if ($defaultBranches -Contains $branch) {
if(git status --porcelain | Where-Object {$_ -match '^\?\?'}){
# untracked files exist
Write-Host "There are untracked files"
Write-Host "Current branch: " $branch
}
elseif(git status --porcelain | Where-Object {$_ -notmatch '^\?\?'}) {
# uncommitted changes
Write-Host "There are uncommitted changes"
Write-Host "Current branch: " $branch
}
else {
# tree is clean
$branch= &git rev-parse --abbrev-ref HEAD
$head = &git rev-parse HEAD
$origin = &git rev-parse origin/HEAD
if ($head -ne $origin) {
Write-Host "Pulling changes on branch " $branch
git pull
} else {
Write-Host "Already up to date"
}
}
} else{
Write-Host "Not on master/develop, will not pull changes automatically"
}
Pop-Location
}
$folders = Get-ChildItem -Path . -Filter .git -Recurse -Depth 1 -Force -Directory |
Select-Object -expandproperty fullname
$defaultBranches = ("master", "develop")
Foreach ($i in $folders)
{
Write-Host "Git fetch: " $i
Push-Location $i
git fetch --prune
Get-Changes
Pop-Location
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment