Scripting git, I need to find out the checked-out branch name. So far it seems the only "reliable" way to do that is with git branch | sed -n '/^\* /s///p'
. (Scare quotes because of things like color.branch
or column.branch
in .gitconfig; it's not reliable at all.) The only other thing I found is git name-rev
, but that seems to return the first (sorted by names) branch that points to HEAD:
> git checkout master
> git checkout -b another
> git checkout master
> git name-rev HEAD
HEAD another
Is there something better than sed -n '\#^ref: refs/heads/#s###p' .git/HEAD
to figure out the checked out branch?