我正在使用下面的命令来查找在我的存储库中是否存在一个带有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>
其他回答
差不多了。
只要忽略verify和quiet如果分支存在,你就得到哈希值如果分支不存在,你就什么都得不到。
将其赋值给一个变量并检查是否为空字符串。
exists=`git show-ref refs/heads/<branch-name>`
if [ -n "$exists" ]; then
echo 'branch exists!'
fi
在windows批处理脚本有点不同,
git rev-parse --verify <branch>
if %ERRORLEVEL% == 0 (
echo "Yes"
) else (
echo "No"
)
是的,有一个。
git rev-parse [<options>] <args>…
请参阅https://git-scm.com/docs/git-rev-parse,在那里可以找到参数集和函数。
git rev-parse --verify <branch-name>
我推荐git show-ref——quiet refs/heads/$name。
——quiet意味着没有输出,这很好,因为这样你就可以干净地检查退出状态。 引用/heads/$name限制到本地分支并匹配完整名称(否则dev将匹配develop)
在脚本中的用法:
if git show-ref --quiet refs/heads/develop; then
echo develop branch exists
fi
git branch --list $branch_name | grep $branch_name
然后检查返回值是0还是1。