git: git branch name programmatically
This is a wiki page. Be bold and improve it!
If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.
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:
# the character $ is being treated as a wildcard and will be expanded, so that the output may include all files in the directory:
echo $production_branch
# Using double quotes solve the problem:
echo "$production_branch"
Issues related to this page:
Project | Summary | Status | Priority | Category | Last updated | Assigned to |
---|---|---|---|---|---|---|
Programming | Bash script: how does the $ wildcard work in gi… | active | normal | feature request | 8 years 44 weeks |