使用批处理(.bat)文件中的竖线运行git.exe命令

时间:2019-05-23 10:58:44

标签: git batch-file

我想使用以下命令从* .bat文件中删除所有git本地分支(master除外):

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

但是此字符串包含“ |”并且命令失败。

也不起作用:

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

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

2 个答案:

答案 0 :(得分:0)

根据here给出的答案,您可以执行以下批处理脚本:

@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

它将使用一个临时文件来存储要删除的分支的名称。

答案 1 :(得分:0)

我使用git别名解决了这个问题。

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

批量运行:

git dellocbr
相关问题