Get git current branch name
you can use rev-parse
for this
git rev-parse --abbrev-ref HEAD
you can also pipe it to pbcopy to put it into your clipboard
git rev-parse --abbrev-ref HEAD | pbcopy
Written by Orlando Del Aguila
Related protips
4 Responses
git branch?
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/47418/0062b4c04fc0c145206a39ef7cd9887c.jpeg)
that returns a list of branches, git rev-parse --abrev-ref HEAD
returns the name of the current branch, so you can pipe it to another command..
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/237608/29090957_440557996372846_2492791919017459712_n.jpg)
For other folks arriving here from a google search, as of Git 1.8.1 you can do git symbolic-ref --short HEAD
to get the current branch name. This may be considered more idiomatic since the docs (https://git-scm.com/docs/git-symbolic-ref) mention using this command for that use case: "Typically you would give HEAD as the <name> argument to see which branch your working tree is on."
git rev-parse --abbrev-ref HEAD and git symbolic-ref --short HEAD does not work in gitlab CI because it checks out a commit rather that a branch.
Instead use this environment variable
CI_COMMIT_REF_NAME=test