HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Get the current branch name in Git

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
gitgetthenamebranchcurrent

Problem

The name of the current branch in Git can be useful for various purposes, such as scripting, debugging, or simply keeping track of your work.
Simply using git rev-parse --abbrev-ref HEAD will print the name of the current branch. You can then pipe this output to other commands or scripts as needed.

Solution

# Syntax: git rev-parse --abbrev-ref HEAD

git checkout patch-1
git rev-parse --abbrev-ref HEAD
# Prints `patch-1`

Code Snippets

# Syntax: git rev-parse --abbrev-ref HEAD

git checkout patch-1
git rev-parse --abbrev-ref HEAD
# Prints `patch-1`

Context

From 30-seconds-of-code: current-branch-name

Revisions (0)

No revisions yet.