Skip to content

Instantly share code, notes, and snippets.

@jsnape
Last active February 1, 2023 21:29
Show Gist options
  • Save jsnape/f09a25c16f5c6ab07ce2c9aaa488243f to your computer and use it in GitHub Desktop.
Save jsnape/f09a25c16f5c6ab07ce2c9aaa488243f to your computer and use it in GitHub Desktop.
Git PowerShell Functions
# This one fetches the latest, prunes remote tracking branches,
# updates main and returns to your current branch ready for you
# to merge from main/origin
function gitrefresh {
param ([string] $mainline = 'main')
## Save the current branch
$gitBranch = & git rev-parse --abbrev-ref HEAD
git checkout $mainline
git pull --rebase --prune
git checkout $gitBranch
}
# Looks for branches that have been deleted on the remote
# Deletes any it finds locally.
# NB: If the branch is not fully merged then you might need to -Force it
# Also, given the destructive nature of this you can -WhatIf or -Confirm too.
# Finally, it will never delete master, develop or the current branch
function gitclean {
[CmdletBinding(SupportsShouldProcess)]
param ([switch] $Force)
$delete = 'd'
$command = "delete branch"
if ($Force) {
$delete = 'D'
$command = "force delete branch!"
}
git.exe branch -vv |
Select-String ': gone' |
ForEach-Object {
$branch = $_.toString().Trim().Split(' ')[0]
if ('master', 'main', 'develop', '*' -contains $branch) {
return
}
if ($PSCmdlet.ShouldProcess($branch, $command)) {
git branch -$delete $branch
}
else {
Write-Output $branch
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment