A Remote Repository is a Git repository hosted on websites like GitHub and BitBucket. We can clone these remote repositories to our local machines.
Sometimes, we may require the original URL of the remote repo. We may need this URL for scripting purposes. Or, the remote repo may have been forked multiple times, and we may not find the original one.
There are multiple methods of finding the URL from where we cloned our repo. Let's learn how to find out the remote repo URL.
The .git/config file, as the name suggests, stores configuration details for our repository. It also holds the remote repository URL. A typical .git/config file is shown below. The URL is present under the [remote "origin"] heading. Origin is the default name for a remote repository. If you have multiple remotes, then search the file according to your remote name.
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = https://github.com/alice/Demo
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "feature"]
remote = origin
merge = refs/heads/feature
We can either directly open the config file. We can also run the cat command from Git Bash to view the contents of the .git/config file.
cat .git/config
The Git Config command helps us to set the repository and global configurations. The --get option is used to fetch configuration details. As we saw in the previous section, the remote repo URL is stored under the remote "origin" heading. We need to pass this heading to the Git Config --get command.
git config --get remote.origin.url
If you are using a different remote name, then replace origin with your remote name.
We can also use the git ls-remote command to find the remote repository URL. We need to use the --get-url option with this command.
git ls-remote --get-url <remote-name>
The Git Remote command helps us to manage remote repository connections. We can use the Git Remote command to find the URL from where we cloned our repo. Unlike the methods discussed above, this command can display additional information.
If we only wish to view the URL, we can use get-url with this command. We also need to mention the remote name. In most cases, the Remote name will be the origin.
git remote get-url <remote-name>
The --verbose option(-v for short) will display some additional information. This option is helpful when we want to know the Push or Fetch URLs, and multiple URLs are present for a particular remote. Also, note that this command will output the details for all the existing remotes(not just the origin).
git remote --verbose
git remote -v # -v short for --verbose
We can also use the Git Remote Show command to view the URL and other details about a remote. It will output the Fetch and Push URLs. Additionally, it outputs details about the remote branches, local branches, and references.
git remote show <remote-name>
Note that this command doesn't work if there is no internet access. However, we still get the URL.
git remote show origin # when there is no internet access
Output:
fatal: unable to access 'https://github.com/alice/Demo/': Could not resolve host: github.com
Let's view and compare the outputs of the different methods that we discussed. Our remote name is origin.
The Git Config --get command displays only the remote repository URL.
git config --get remote.origin.url
Output:
https://github.com/alice/Demo
Like the Git Config command, the Git Ls-Remote command only outputs the URL.
git ls-remote --get-url origin
Output:
https://github.com/alice/Demo
The Git Remote command with get-url also outputs just the URL.
git remote get-url origin
Output:
https://github.com/alice/Demo
The Git Remote command with the --verbose option(or -v flag) will output the Push and Fetch URLs for all the existing remotes. In the output below, we can see that there are two remotes(origin and demo-remote). The output will contain the Push and Fetch URLs of both the remotes.
git remote --verbose
Output:
demo-remote https://github.com/bob/DemoRepo (fetch)
demo-remote https://github.com/bob/DemoRepo (push)
origin https://github.com/alice/Demo (fetch)
origin https://github.com/alice/Demo (push)
The Git Remote Show command displays the most information about the remote repository. It includes the Push and Fetch URLs, remote branch information, local branch configurations for Git Pull, and local reference configurations for Git Push.
git remote show origin
Output:
* remote origin
Fetch URL: https://github.com/alice/Demo
Push URL: https://github.com/alice/Demo
HEAD branch: master
Remote branches:
feature tracked
master tracked
Local branches configured for 'git pull':
feature merges with remote feature
master merges with remote master
Local refs configured for 'git push':
feature pushes to feature (up to date)
master pushes to master (up to date)
We need the URL to clone a remote repository. Git stores this URL and additional information about the remote repository and the remote connection.
There are multiple methods to get the remote repo's URL. The git config --get command is preferred for scripting. We can also view the URL in the .git/config file. The Git Remote command with get-url also outputs the URL. We can use the Git Remote command with the --verbose option if we want additional information. Or we can use the Git Remote Show command, which displays a lot of supplementary information. Note that the Git Remote Show command will not work if you do not have internet access.