Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


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.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Comments are closed on this article!