current git branch

Very often, it's useful to get the current branch of your git repository at the command line, or via a script. The obvious (and messy) way is to run git branch and capture the line with the asterisk and strip the asterisk and whitespace. Here's a better way:

git rev-parse --abbrev-ref HEAD

# or assign it to a variable
branch=$(git rev-parse --abbrev-ref HEAD)

You can even add this to your PS1 environment variable so that your command prompt always shows the branch you're in.

Comments !

blogroll

social