DEV Community

Simon Reynolds
Simon Reynolds

Posted on • Originally published at simonreynolds.ie on

Morning routine - updating git repos

  • Get to work
  • Clock in
  • Make coffee
  • Fetch latest changes from git.... Morning routine - updating git repos

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
}
view raw Fetch-All.ps1 hosted with ❤ by GitHub

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay