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?
Just output the branch you are on with:
git rev-parse --symbolic-full-name --abbrev-ref HEAD
There should be no trouble also if you have more than one branches, and if you aren't on any branch it just gives you HEAD
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With