How to Get a Local Repo's Remote URL in Git


How can we obtain a local Git repository’s remote URL?

Least information: git config or git ls-remote

We can easily get the remote URL using git config or git ls-remote.

git config --get remote.origin.url
# OR
git ls-remote --get-url

We don’t need to be connected to any network to get this value as it’s stored in our git configuration (.git/config), which means this value can be obtained and used in scripts.

The output might look something like this:

git@github.com:username/repository.git

More information: git remote -v

If we want slightly more information, such as the push and fetch URLs, we can use git remote -v.

git remote -v

The output might look something like this:

origin  git@github.com:username/repository.git (fetch)
origin  git@github.com:username/repository.git (push)

Most information: git remote show origin

If we want even more information, we can use git remote show.

git remote show origin

Unlike the first two methods, we need to be connected to a network to obtain this information.

The output might look something like this:

* remote origin
  Fetch URL: git@github.com:username/repository.git
  Push  URL: git@github.com:username/repository.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)