I have many habits during the day, and the most important of all is my morning routine.
As you see below I use Microsoft ToDo to remind me what I need to do each morning and one of the steps I need to do is to update my local GitHub repositories.
I have many of that and of course it's not possible to do it manually.
This is the reason why I have created a PowerShell script that works for me.
You can copy my code and put the script in the main folder of your GitHub repositories and launch it from the command line.
It takes more or less 3-5 minutes (but it depends on how many repositories you have locally).
$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 |
As you can see in the script, I use the command "git fetch prune".
This command helps to stay with clean branches and folders.
You can learn more on this topic at this link.
In the video below you can see the script running on my machine.
Feel free to share your feedback or make some changes to the script.
Top comments (3)
Hi, this is cool to keep your local repositories up to date. I created recently something to help devs perform changes across multiple repos, it’s just a basic find & replace but I just want to share it here if that’s fine. gitbulk.com
thanks for sharing!
Hey. Thanks for the script. I know this is old post and people who are using mac/linux had already developed the similar script (bash file) for themselves. Though I am posting
bash-script
which is running fine in my macbook. So if incase someone is looking for this post in future, it might help:Feel free to share your feedback/make some changes in this script.
PS: I have commented out this code in my script:
All bin & obj files and folders are removed.