How do I get the current branch name in Git?

In this article you will learn how do I get the current branch name in Git?

 

To get the get the branch name in Git can use the command git rev-parse --abbrev-ref HEAD to get the current branch name in Git.

This command uses git rev-parse to resolve the HEAD reference to its commit hash, and --abbrev-ref option to display only the branch name and not the full commit hash.

To display only the name of the current branch you're on:

git rev-parse --abbrev-ref HEAD

 

 

 

You can also use git branch command which will list all the branches along with the current branch which will be marked by an asterisk (*)

 

git branch
* current-branch
  another-branch

 

 

 

 

You can also use git status command, which will show the current branch name in the first line of the output.

 

$ git status
On branch current-branch

You can also use git symbolic-ref --short HEAD command which will return the current branch name.

 

 

How to Show All Remote and Local Branch Names

 

To list down All Remote and Local Branch Names You can use the command git branch -a to show all branches (both local and remote branches) in Git. The -a option tells Git to show all branches, including remote branches that are not currently checked out.

Here is an example:

 git branch -a
  master
* feature-branch
  remotes/origin/master
  remotes/origin/feature-branch

 

 

You can see in the above example that local branches are denoted by a single * character and remote branches are prefixed with the remote name (e.g., remotes/origin/).

 

 

 

Another way to show the local and remote branches is by using git branch -r command to show all remote branches and git branch -l command to show all local branches.

 

$ git branch -r
  origin/master
  origin/feature-branch

$ git branch -l
* master
  feature-branch

 

 

 

You can also use git branch -a command to show all branches and git branch -r and git branch -l to show remote and local branches respectively.

 

  git branch -a
  master
* feature-branch
  remotes/origin/master
  remotes/origin/feature-branch

 

 

 

Another way is by using git ls-remote --heads to show all remote branches, This command will show all branches of the remote repository.

 

git ls-remote --heads 

 

 

 

Where is the name of the remote repository.

 

git remote show will also show all branches of the remote repository.

 

git remote show 

 

 

git branch -vv command will show the local branches, it's upstream branch, and the last commit made on that branch.

 

git branch -vv

 

 

You can use any of the above commands to show all branches, remote and local.


Tags:

git

Share:

Related posts