我正在使用下面的命令来查找在我的存储库中是否存在一个带有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 show-branch <BRANCH-NAME> &>/dev/null && echo yes || echo no
其他回答
在脚本中使用:
git show-ref -q --heads <branch-name>
当且仅当<branch-name>存在时,该函数将退出0 作为一个地方分支机构。
例子:
if git show-ref -q --heads <branch-name>; then
echo 'Branch exists'
fi
这是我实现的方法,看起来更稳定
$branchExists = git ls-remote $gitUrl $gitBranch
if(!([bool]$branchExists))
{
Write-Host "branch $branchName does not exist"
}
else
{
Write-Host "branch $branchName exists"
}
如果分支存在,则branchExist变量的内容将类似于:
hashcode of branch name feature/my_branch
对于在脚本中使用,我建议使用以下命令:
git ls-remote --heads <repo_url> "<branch_name>" | wc -l
注意,<repo_url>可以只是一个“。”来指定本地回收(如果您在其目录结构中)、本地回收的路径或远程回收的地址。
如果<branch_name>不存在,则该命令返回0,如果存在则返回1。
让我们将其称为git is_localbranch(您需要在.gitconfig中添加别名)。
用法:
$ git is_localbranch BRANCH
来源:
git branch | grep -w $1 > /dev/null
if [ $? = 0 ]
then
echo "branch exists"
fi
git show-branch <BRANCH-NAME> &>/dev/null && echo yes || echo no