Can I delete all the local branches except the current one?

后端 未结 14 2313
广开言路
广开言路 2020-12-12 13:07

I want to delete all branches that get listed in the output of ...

$ git branch

... but keeping current branch, in one step. Is th

14条回答
  •  时光说笑
    2020-12-12 13:36

    I once created this construct for my Windows environment. Maybe it'll help someone else. During execution, the master and current branch are not deleted. All other merged branches will be deleted regardless.

    @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
    

提交回复
热议问题