How to programmatically determine the current checked out Git branch

Viewed 145383
20 Answers

Same results as accepted answer in a one-line variable assignment:

branch_name=$((git symbolic-ref HEAD 2>/dev/null || echo "(unnamed branch)")|cut -d/ -f3-)
Related