Roman
Roman

Reputation: 11

Running git.exe commands with vertical bar from batch (.bat) file

I want to delete all git local branches (except master) from *.bat file using command:

git.exe branch | grep -v "master" | xargs git branch -D 

But this string includes "|" and the command fails.

Also doesn't work:

git.exe "branch | grep -v "master" | xargs git branch -D"

and

git.exe branch ^| grep -v "master" ^| xargs git branch -D

Upvotes: 0

Views: 177

Answers (2)

Roman
Roman

Reputation: 11

I solved this problem with using git alias.

[alias]
    dellocbr = !sh -c \"git branch | grep -v \"master\" | xargs git branch -D\" -

Run into batch:

git dellocbr

Upvotes: 1

BenWayne182
BenWayne182

Reputation: 94

Based on the answer given here, you can do a batch script like this :

@echo off
REM -- Variable declerations
set "textFile=tempBranchInfo.txt"
set "branchToKeep=master"
set "branchToReplaceWith="
git checkout master
git branch > %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

It gonna use a temporary file to store the name of the branches to delete.

Upvotes: 0

Related Questions