Git plumbing to tell written branch

Scripting git, I need to find out the name of the branch branch. So far this is the only "reliable" way to do this with git branch | sed -n '/^\* /s///p' git branch | sed -n '/^\* /s///p' . (Quotes quotes due to things like color.branch or column.branch in .gitconfig; it is not reliable at all). The only thing I found is git name-rev , but it seems to return the first branch (sorted by name) that points to HEAD:

 > git checkout master > git checkout -b another > git checkout master > git name-rev HEAD HEAD another 

Is there anything better than sed -n '\#^ref: refs/heads/#s###p' .git/HEAD to figure out the marked branch?

+4
source share
2 answers

Here is a small git call that I used in several scripts that either return refs/heads/branchname , or if you are not on a branch, this gives the SHA of your individual HEAD:

 cur_branch=$(git symbolic-ref HEAD 2>> /dev/null || git rev-parse HEAD) 

Removing the refs/heads/ prefix should be pretty simple if you need it ...

+5
source

Just enter the branch you are in:

 git rev-parse --symbolic-full-name --abbrev-ref HEAD 

There should be no problem if you have multiple branches, and if you are not on any branch, it just gives you HEAD

+6
source

Source: https://habr.com/ru/post/1483796/


All Articles