gpt4 book ai didi

git - 在批处理脚本中使用 git 命令时,如何因错误而失败?

转载 作者:行者123 更新时间:2023-12-05 04:05:26 25 4
gpt4 key购买 nike

下面是我在其中执行一组 git 操作的批处理脚本,我如何在 git checkout “Branch already exits” 等 git 错误上失败

```

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.

最佳答案

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 检查之前的命令是否有错误;它确实检查错误级别是否 >= 1。

关于git - 在批处理脚本中使用 git 命令时,如何因错误而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51199243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com