我正在使用下面的命令来查找在我的存储库中是否存在一个带有branch-name的本地git分支。这对吗?有没有更好的办法?
请注意,我是在脚本中执行此操作的。因此,如果可能的话,我希望使用管道命令。
git show-ref --verify --quiet refs/heads/<branch-name>
# $? == 0 means local branch with <branch-name> exists.
我正在使用下面的命令来查找在我的存储库中是否存在一个带有branch-name的本地git分支。这对吗?有没有更好的办法?
请注意,我是在脚本中执行此操作的。因此,如果可能的话,我希望使用管道命令。
git show-ref --verify --quiet refs/heads/<branch-name>
# $? == 0 means local branch with <branch-name> exists.
当前回答
是的,有一个。
git rev-parse [<options>] <args>…
请参阅https://git-scm.com/docs/git-rev-parse,在那里可以找到参数集和函数。
git rev-parse --verify <branch-name>
其他回答
是的,有一个。
git rev-parse [<options>] <args>…
请参阅https://git-scm.com/docs/git-rev-parse,在那里可以找到参数集和函数。
git rev-parse --verify <branch-name>
在脚本中使用:
git show-ref -q --heads <branch-name>
当且仅当<branch-name>存在时,该函数将退出0 作为一个地方分支机构。
例子:
if git show-ref -q --heads <branch-name>; then
echo 'Branch exists'
fi
git show-ref和git rev-parse都不能在我的情况下工作。
$ git --version
git version 2.21.0
$ git show-branch --list
* [master] mybranch commit
$ BRANCH_NAME=mybranch
$ git rev-parse --verify $BRANCH_NAME
fatal: Needed a single revision
$ git show-ref refs/heads/$BRANCH_NAME
<no otput>
$ [ $? == 0 ] && echo "$BRANCH_NAME exists" || echo "$BRANCH_NAME not exists"
mybranch not exists
最后得到了这个
$ BRANCH_NAME=mybranch
$ SHOW_ALL=`git show-branch --all | grep -w $BRANCH_NAME`
$ [ $? == 0 ] && echo "$BRANCH_NAME exists" || echo "$BRANCH_NAME not exists"
mybranch exists
您也可以使用脚本文件
#!/bin/sh
BRANCH_NAME=mybranch
if grep -Fqe $BRANCH_NAME << EOF
`git show-branch --all`
EOF
then
echo "$BRANCH_NAME exists"
else
echo "$BRANCH_NAME not exists"
fi
Git branch -l <branch-name>
如果分支存在,则返回分支名称;如果分支不存在,则返回none
git show-branch <BRANCH-NAME> &>/dev/null && echo yes || echo no