[alias]
# Delete all local branches but master and the current one, only if they are fully merged with master.
br-delete-useless = "!f(){\
git branch | grep -v "master" | grep -v ^* | xargs git branch -d;\
}; f"
# Delete all local branches but master and the current one.
br-delete-useless-force = "!f(){\
git branch | grep -v "master" | grep -v ^* | xargs git branch -D;\
}; f"
我曾经为我的 Windows 环境创建了这个结构,也许它可以帮助其他人。
在执行期间,不删除主分支和当前分支。所有其他合并的分支都将被删除。
@echo off
cd PATH_TO_YOUR_REPO
REM -- Variable declerations
set "textFile=tempBranchInfo.txt"
set "branchToKeep=master"
set "branchToReplaceWith="
git branch --merged > %textFile%
REM -- remove "master" from list to keep the branch
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
>>"%textFile%" echo(!line:%branchToKeep%=%branchToReplaceWith%!
endlocal
)
REM -- execute branch delete commands
for /f "delims=" %%a in (%textFile%) do (
git branch -D %%a
)
REM -- remove temp-file with branch information inside
DEL %textFile%
REM -- show local branches after the cleaning
echo Local branches:
git branch
pause
exit