How to Get the Name of a Git Repository

John Wachira Feb 02, 2024
How to Get the Name of a Git Repository

This article illustrated how we could get the name of any Git repository. When we clone a Git repository, it is usually represented as a folder in our local machine.

Hence, the folder should give you an idea of the Git repository’s name. Nonetheless, Git has a few commands that can display the name of a Git repo.

Get the Name of a Git Repository

As we mentioned, a Git repository’s name is usually represented by the root directory of your project. To display the name, you can run the command below.

$ basename `git rev-parse --show-toplevel`

The git rev-parse --show-toplevel part will display the path to the project’s root directory. The basename will remove the first part of the path to give you something like this.

$ basename `git rev-parse --show-toplevel`1~
flask-corona-dark1~

You can also run the command below.

$ basename $(git remote get-url origin)

This is the most efficient and reliable command since it displays the repository name for the remote. Here is an example.

$ basename $(git remote get-url origin)
flask-corona-dark.git

In a nutshell, you can get the name of any Git repository. The root directory usually points to the repository’s name if you had cloned from a remote.

The git remote get-url origin command will display the repository name for your remote.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn