Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git plumbing to tell the checked out branch

Tags:

git

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?

like image 282
just somebody Avatar asked Dec 07 '22 07:12

just somebody


1 Answers

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

like image 98
bpoiss Avatar answered Dec 22 '22 21:12

bpoiss