在批处理脚本中使用git命令时,如何在出现错误时终止操作?

3
以下是我执行一系列git操作的批处理脚本,如何在出现git错误(例如在git checkout时出现“分支已存在”)时使其失败?
``` ```
REM arg 1 = branch to copy
REM arg 2 = branch in which copy will be merged
set arg1=%1%
set arg2=%2%
echo copying %arg1% to %arg2%
echo "check out as tmp "+%arg1%
set ret = git checkout -b tmp %arg1%
echo %ret%
set ret = git checkout -b tmp %arg1%
echo %ret%
exit 0
REM emitted code for brevity 

``` output :

c:\my-project>c:\bat\git-copy-branch.bat master my-git-branch

c:\my-project>git checkout -b tmp master
fatal: A branch named 'tmp' already exists.

c:\my-project>git checkout -b tmp master
fatal: A branch named 'tmp' already exists.

c:\my-project>git checkout -b tmp master
fatal: A branch named 'tmp' already exists.

1
我不确定你是如何得到任何GIT输出的,因为你展示的代码从未执行过GIT。此外,你的SET命令是不正确的。你需要在=符号前后删除空格。 - Squashman
@Squashman 噢...不好意思..那是我在尝试的东西..你是对的,设置命令set ret = git checkout....没有做任何事情..我正在纠正..谢谢 - old-monk
1个回答

3
REM arg 1 = branch to copy
REM arg 2 = branch in which copy will be merged
set arg1=%1%
set arg2=%2%
echo copying %arg1% to %arg2%
echo "check out as tmp "+%arg1%
git checkout -b tmp %arg1%
if errorlevel 1 goto Quit
echo Ok
git checkout -b tmp %arg1%
if errorlevel 1 goto Quit
echo Ok
:Quit

:Quit是一个标签。 if errorlevel 1检查前一个命令是否有任何错误;它实际上检查的是errorlevel是否大于等于1。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接