Skip to content

Instantly share code, notes, and snippets.

@kasuken
Last active March 27, 2024 05:53
Show Gist options
  • Save kasuken/e128dc563005d13a5065d1d98162eef6 to your computer and use it in GitHub Desktop.
Save kasuken/e128dc563005d13a5065d1d98162eef6 to your computer and use it in GitHub Desktop.
A script to update your local GitHub repositories with the remote branches (and cleanup)
$branches = ("master", "main", "develop", "dev")
# get all directories in the current directory (just the first level)
$repos = Get-ChildItem -Path . -Filter .git -Recurse -Depth 1 -Force -Directory | Select-Object -expandproperty fullname
function Update-Repos {
Push-Location ".."
$branch = &git rev-parse --abbrev-ref HEAD
Write-Host "Current branch:" $branch
if ($branches -Contains $branch) {
if (git status --porcelain | Where-Object { $_ -match '^\?\?' }) {
# untracked files exist
Write-Host "There are untracked files" -ForegroundColor DarkYellow
}
elseif (git status --porcelain | Where-Object { $_ -notmatch '^\?\?' }) {
# uncommitted changes
Write-Host "There are uncommitted changes" -ForegroundColor DarkYellow
}
else {
# all clear
$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 -ForegroundColor Green
git pull
}
else {
Write-Host $branch "is up to date" -ForegroundColor Green
}
}
}
else {
Write-Host "Not one of the default branches. Pull it manually." -ForegroundColor Yellow
}
Pop-Location
}
Write-Host "Starting..." -ForegroundColor Green
Foreach ($i in $repos) {
# https://www.git-tower.com/learn/git/faq/cleanup-remote-branches-with-git-prune
Write-Host "Git fetch prune:" $i
Push-Location $i
git fetch --prune
Update-Repos
Pop-Location
Write-Host "----------------------------------------------------" -ForegroundColor White
}
Write-Host "Your GitHub folder is up to date." -ForegroundColor Green
Write-Host "----------------------------------------------------" -ForegroundColor White
Write-Host "----------------------------------------------------" -ForegroundColor White
Write-Host "Removig bin & obj folders" -ForegroundColor White
Get-ChildItem .\ -include bin,obj -exclude node_modules -Recurse | ForEach-Object ($_) {
Write-Host "Removing:" $_.fullname -ForegroundColor Red
remove-item $_.fullname -Force -Recurse
}
Write-Host "All bin & obj files and folders are removed." -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment