Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从批处理(.bat)文件运行带有竖线的git.exe命令_Git_Batch File - Fatal编程技术网

从批处理(.bat)文件运行带有竖线的git.exe命令

从批处理(.bat)文件运行带有竖线的git.exe命令,git,batch-file,Git,Batch File,我想使用以下命令从*.bat文件中删除所有git本地分支(主分支除外): git.exe branch | grep -v "master" | xargs git branch -D 但该字符串包含“|”,命令失败 也不起作用: git.exe "branch | grep -v "master" | xargs git branch -D" 及 根据给出的答案,您可以执行如下批处理脚本: @echo off REM -- Variable declerations set "textF

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

git.exe branch | grep -v "master" | xargs git branch -D 
但该字符串包含“|”,命令失败

也不起作用:

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

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

@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

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

我使用git alias解决了这个问题

[alias]
    dellocbr = !sh -c \"git branch | grep -v \"master\" | xargs git branch -D\" -
批量运行:

git dellocbr

命令如何失败?您看到了什么错误消息?
git dellocbr