Last active
February 1, 2023 21:29
-
-
Save jsnape/f09a25c16f5c6ab07ce2c9aaa488243f to your computer and use it in GitHub Desktop.
Git PowerShell Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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