A script that creates a branch should first check whether it exists already. Grepping the output of git branch breaks on names that share a prefix. show-ref checks the exact ref.
git show-ref --verify --quiet refs/heads/feature
echo $?
With --verify the ref must match exactly, and --quiet prints nothing, so only the exit code is left: 0 when it exists, 1 when it doesn’t. In a test repository the check for an existing branch gives 0 and the one for a branch that isn’t there gives 1. Leave out --quiet and it prints the commit hash and the full name.
if git show-ref --verify --quiet refs/heads/feature; then
git switch feature
else
git switch -c feature
fi
Use the full ref name. refs/heads/ is for local branches, refs/tags/ for tags and refs/remotes/origin/ for remote-tracking ones. With a short name like feature, --verify fails.
Caveat: it only looks at refs in your local repository, so a branch that exists on the server but was never fetched counts as missing. Run git fetch first, or use git ls-remote --exit-code to ask the remote itself.
Newer Git versions also have git show-ref --exists refs/heads/feature, which checks that the ref exists without resolving it. It exits with 0 when the ref exists and 2 when it is missing, and prints an error message in that case. The Git 2.43.0 manual lists it, so check git show-ref -h on your own version first; where it is missing, stay with --verify --quiet.
Further reading: git-show-ref documentation