How To Find The Original URL of a Local Git Repository

Avatar

By squashlabs, Last Updated: October 27, 2023

How To Find The Original URL of a Local Git Repository

To find the original URL of a local Git repository, you can use the git remote show origin command. This command displays information about the remote repository associated with the local repository, including the URL. Here are the steps to find the original URL of a local Git repository:

Step 1: Open the Terminal or Command Prompt

Open the Terminal or Command Prompt on your computer. This is where you will enter the Git commands to find the original URL of the local repository.

Related Article: How to Delete a Remote Tag in Git

Step 2: Navigate to the Local Repository

Navigate to the directory that contains the local Git repository you want to find the original URL for. Use the cd command followed by the directory path to navigate to the correct location. For example:

cd /path/to/repository

Replace /path/to/repository with the actual path to your local Git repository.

Step 3: Run the git remote show origin Command

Run the git remote show origin command to display information about the remote repository associated with the local repository. This command will show the URL of the original remote repository. Here’s an example:

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

In the output, look for the lines that start with “Fetch URL” and “Push URL”. These lines will contain the original URL of the remote repository.

Step 4: Note Down the Original URL

Note down the original URL of the remote repository displayed in the output of the git remote show origin command. You can either copy the URL or write it down for future reference. The URL will typically start with https:// or git@.

Related Article: How to Git Pull from a Specific Branch

Alternative Method: Check the Git Config File

If the git remote show origin command doesn’t provide the desired information, you can also check the Git config file directly. The Git config file contains the configuration settings for the local repository, including the URL of the remote repository. Here’s how you can check the Git config file:

Step 1: Open the Terminal or Command Prompt

Open the Terminal or Command Prompt on your computer.

Step 2: Navigate to the Local Repository

Navigate to the directory that contains the local Git repository you want to find the original URL for. Use the cd command followed by the directory path to navigate to the correct location. For example:

cd /path/to/repository

Replace /path/to/repository with the actual path to your local Git repository.

Step 3: Open the Git Config File

Open the Git config file using a text editor. The Git config file is located in the .git directory of the local repository. Use the following command to open the file:

vi .git/config

This command opens the Git config file in the Vi text editor. You can replace vi with any other text editor of your choice.

Step 4: Find the URL in the Config File

Search for the URL of the remote repository in the Git config file. Look for a section that starts with [remote "origin"] and contains the line url = <URL>. The <URL> will be the original URL of the remote repository.

Step 5: Note Down the Original URL

Note down the original URL of the remote repository found in the Git config file. You can either copy the URL or write it down for future reference.

Best Practices

When finding the original URL of a local Git repository, it’s important to follow these best practices:

– Use the git remote show origin command as the primary method to find the original URL of a local Git repository. This command provides detailed information about the remote repository, including the URL, branches, and more.
– If the git remote show origin command doesn’t provide the desired information, only then consider checking the Git config file directly.
– Make sure you have the necessary permissions to access the remote repository. Without the proper permissions, you won’t be able to fetch or push changes to the remote repository.
– Double-check the URL to ensure it is correct and complete. A missing or incorrect URL can cause issues when fetching or pushing changes to the remote repository.

More Articles from the Git Tutorial: From Basics to Advanced Concepts series:

How to Download a Single Folder from a Github Repo

Downloading a single folder from a GitHub repository using Git can be done in two ways: using the GitHub website or using the Git command-line tool. Additionally, there... read more

How to Login to a Git Remote Repository

Logging in to Git is an essential skill for any software developer. This article provides a step-by-step guide on how to navigate the login process, from installing Git... read more

How to Create a Tag in a GitHub Repository

Creating a tag in a GitHub repository is an essential step in code versioning. This step-by-step guide will walk you through the process, from cloning the repository to... read more

How to Clone a Git Repository Into a Specific Directory

Cloning a Git repository into a specific directory is a fundamental skill for software developers. This step-by-step guide provides two methods for accomplishing this... read more

How to Undo/Revert a Git Commit Before Push

When working with Git, it's important to know how to undo a commit before pushing it to the repository. This article provides a simple guide on removing a Git commit... read more

How to Revert a Pushed Merge Commit in Git

Reverting a pushed merge commit in Git can be a daunting task, but with the right approach, it can be done efficiently. In this article, we provide a step-by-step guide... read more