-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGit_Local_Removal.ps1
More file actions
28 lines (23 loc) · 1.03 KB
/
Copy pathGit_Local_Removal.ps1
File metadata and controls
28 lines (23 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<#
.SYNOPSIS
This script removes local Git branches that no longer exist on the remote repository.
.DESCRIPTION
It fetches the latest state of the remote repository, identifies local branches that are not present
on the remote, and deletes them to keep the local repository clean.
.EXAMPLE
Git_Local_Removal.ps1
#>
if (-not (test-path -path './.git' -PathType Container)) {
Write-Error "The current directory is not a Git repository. Please navigate to a valid Git repository and try again."
exit 1
}
# Remove locally tracked branches that no longer exist on the remote repository
git fetch --prune
$RemoteBranches = git branch -r | Where-Object { $_ -notmatch 'HEAD' } | ForEach-Object { $_.Trim().Replace('origin/', '') }
$LocalBranches = git branch | where-object { $_ -notmatch '^\*' } | ForEach-Object { $_.Trim() }
# Identify local branches that no longer exist on the remote and deletes them
foreach ($LocalBranch in $LocalBranches) {
if ($RemoteBranches -notcontains $LocalBranch) {
git branch -d $LocalBranch
}
}