我需要使用Shell脚本创建Git分支,但由于该分支可能已经存在,所以我需要意识到这一点。目前我正在使用:
if [ `git branch | grep $branch_name` ]
then
echo "Branch named $branch_name already exists"
else
echo "Branch named $branch_name does not exist"
fi
问题在于 grep
命令查找分支名称时没有匹配到完全相同的名称,也就是说,如果我执行 grep name
,那么名称为 branch-name
的分支也会被匹配到。
那么有更好的方法来解决这个问题吗?
谢谢!