In a bash script, you can get the current branch's name programmatically thus:
#!/bin/bash
branch_name=$(git symbolic-ref -q HEAD)
echo $branch_name
branch_name=${branch_name##refs/heads/}
echo $branch_name
branch_name=${branch_name:-HEAD}
echo $branch_name
Beware, to find a branch name (whether checked out or not) according to a pattern:
#!/bin/bash
git checkout production.release.4.2
production_branch=`git branch | grep "production"`
# May not give the expected result: