在批处理脚本中使用git命令时,如何出错?

时间:2018-07-05 20:18:36

标签: git batch-file

下面是我在其中执行一组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 

``` 输出:

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 个答案:

答案 0 :(得分:1)

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 errorlvel 1 goto Quit
echo Ok
git checkout -b tmp %arg1%
if errorlvel 1 goto Quit
echo Ok
:Quit

:Quit是一个标签。 if errorlvel 1检查上一个命令是否有错误;它真的检查errorlvel> = 1。