Skip to content

Instantly share code, notes, and snippets.

@projected1
Created August 9, 2020 10:56
Show Gist options
  • Save projected1/56ca6f62c1a4090c2bdaab62294fbdc8 to your computer and use it in GitHub Desktop.
Save projected1/56ca6f62c1a4090c2bdaab62294fbdc8 to your computer and use it in GitHub Desktop.
Locates branches that were already merged and deletes them from the remote GIT repository.
@rem ------------------------------------------------------
@rem Locates branches that were already merged and deletes
@rem them from the remote GIT repository.
@rem
@rem Usage: git_branch_cleanup.bat [--force]
@rem --force - delete the remote branch. optional.
@rem if not specified, performs dry run
@rem by default.
@rem ------------------------------------------------------
@echo off
setlocal enabledelayedexpansion
set tmpfile=%temp%\%random%
set timestamp=%date:~10,4%%date:~7,2%%date:~4,2%%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
for %%i in (.) do set logfile=%~dp0\%%~nxi_%timestamp: =0%.log
for /f "usebackq tokens=*" %%f in (`git branch -r`) do (
for /f "usebackq tokens=*" %%d in (`git branch -r --merged %%f`) do (
if "%%d" neq "%%f" (
set d=%%d
if "!d:~0,15!" neq "origin/release/" (
if "!d:~0,13!" neq "origin/master" (
if "!d:~0,11!" neq "origin/HEAD" (
echo %%d =^> %%f >> %logfile%
echo %%d >> %tmpfile%
)
)
)
)
)
)
echo. >> %logfile%
echo ------------------------------------------------- >> %logfile%
echo. >> %logfile%
for /f "usebackq tokens=*" %%l in (`sort %tmpfile%`) do (
set cur=%%l
if "!cur!" neq "!prev!" (
echo deleting !cur! . . . >> %logfile%
if "%1" == "--force" (
git push origin -d !cur:~7!
)
)
set prev=%%l
)
endlocal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment