How to show the current branch name in Git?

In Git, there are three ways to get the current branch. All of them are explained below:

  1. If you run git status, you will find a line that shows "On branch master". Which means you are in the master branch.
$git status
On branch master
nothing to commit, working directory clean

If you are in the testing branch, you will find "On branch testing".

  1. When you run git branch without a branch name, you will get the list of branches. The active branch will have a star next to it.
$git branch
* master
  testing

Here, the active branch is master.

  1. If you run git log, you will see a line at a top that shows (HEAD -> master). Which means the master branch is currently checked out and HEAD pointer points to the most recent commit of the master branch.
$git log
commit dc99c45a1b78a8ce4fedd6444136905ab8d81b1b (HEAD -> master)
Author: Mohit Natani 
Date:   Sun Dec 27 07:23:53 2020 +0000

    Initial commit

Sometimes, you may see (HEAD -> testing, master), which means you are in the testing branch. Both the testing and master branches are pointing the same commit.