如何使用git命令检测分支是否存在?

Dee*_*Two 4 git branch

如何使用git命令检测分支是否存在?

在某些 shell 脚本中需要这个

Sve*_*ach 14

要检查 shell 变量中的字符串是否$REVISION正确解析为 git 数据库中的有效对象,可以使用

git rev-parse --quiet --verify $REVISION
Run Code Online (Sandbox Code Playgroud)

如果找到修订版本,这会将 SHA1 打印到标准输出,否则返回非零退出状态,因此您可以在 if 子句中使用它:

if git rev-parse --quiet --verify $REVISION > /dev/null; then
    # whatever
fi
Run Code Online (Sandbox Code Playgroud)

请注意,这不仅允许严格意义上的分支名称,还允许所有有效的修订引用。