- Get to work
- Clock in
- Make coffee
- Fetch latest changes from git....
Going through a half dozen repositories that I regularly interact with involves far more typing than I care to do repeatedly.
What I want is to cycle through each repository and do the following
git fetch --prune
- If I'm on the default branch (usually master or develop) and have no local changes then just
git pull
straight away
Sounds perfect for automation! PowerShell to the rescue, leaving time to savour the coffee
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 | |
} |
Top comments (0)