Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


1. Overview

Git has become a popular and widely used version control system in the industry. Usually, when we work with a Git repository, we work with branches.

In this tutorial, we'll explore how to get the branch name we're currently working on.

2. Introduction to the Problem

First of all, let's prepare a Git repository called myRepo:

$ git branch -a
* feature
  remotes/origin/HEAD -> origin/master

As the git branch command's output shows, we have two local branches in myRepo, and now, the currently checked-out branch is the feature branch, as there is a “*” character in front of “feature“.

Sometimes, we may want to only get the current branch name without the whole branch list. For example, if we have many local branches in the repository, we may get a long branch list. Or, when we write some scripts to automate some processes, we want only to get the branch name and pass it to other commands.

So next, let's see how to get the current branch “feature” quickly.

3. Using the git symbolic-ref Command

The current branch information is stored or linked by the .git/HEAD file, depending on the Git version. For example, on this machine, the .git/HEAD file holds:

$ cat .git/HEAD
ref: refs/heads/feature

So, it points to a symbolic refThe git symbolic-ref command allows us to read and modify symbolic refs. We can use this command to get the short current branch name:

$ git symbolic-ref --short HEAD

4. Using the git rev-parse Command

Since Git version 1.7, we can alternatively use the git rev-parse command to get the current branch name:

$ git rev-parse --abbrev-ref HEAD

5. Using the git name-rev Command

Git's git name-rev command can find the symbolic names for given revs. Therefore, to get the current branch name, we can read the name of the rev HEAD:

$ git name-rev --name-only HEAD

As the output above shows, the git name-rev command prints the current branch name as well. We should note that we added the –name-only option to the command to suppress the rev information (HEAD) from the output.

6. Using the git branch Command

We know if we launch the git branch command without any options, Git will print all local branches and put the current branch on the first line, with a “*” character in front of the name:

$ git branch
* feature

Therefore, we can parse the git branch command's output to get the branch name. Let's see an example of combining git branch and the sed command:

$ git branch | sed -n '1{s/^* *//;p}'

Actually, since version 2.22, Git has introduced the –show-current option to the git branch command so that we can get the current branch name straightforwardly:

$ git branch --show-current

Obviously, this command is better than parsing the git branch command's output.

7. Conclusion

In this quick article, we've addressed several ways to get the current branch name in a Git repository.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


Generic footer banner
Comments are closed on this article!