Try with:
git symbolic-ref --short -q HEAD
Or you try with git branch
with --no-color
force simple plain string the output:
git branch --no-color
With grep in regex mode(-E
) you can check if exists the character '*':
git branch --no-color | grep -E '^\*'
The results its similar to:
* currentBranch
You can use the next options:
sed 's/\*[^a-z]*//g'
cut -d ' ' -f 2
awk '{print $2}'
for example:
git branch --no-color | grep -E '^\*' | sed 's/\*[^a-z]*//g'
git branch --no-color | grep -E '^\*' | sed cut -d ' ' -f 2
git branch --no-color | grep -E '^\*' | awk '{print $2}'
if exists a error you cant use an default value:
cmd || echo 'defualt value';
All into in a bash function:
function get_branch() {
git branch --no-color | grep -E '^\*' | awk '{print $2}' \
|| echo "default_value"
# or
# git symbolic-ref --short -q HEAD || echo "default_value";
}
Use:
branch_name=`get_branch`;
echo $branch_name;
git branch --show-current
. See my answer here. – Severe--no-color
will give a even more script-friendly resultgit branch --no-color --show-current
– Graphics