Git

How do I Change the URI (URL) for a Remote Git Repository?

In Git, “remote” is a pointer that refers to another duplicate Git repository usually hosted on a GitHub server. When developers clone a remote repository, it will be set automatically. However, when Git remote repository is relocated to another host, developers are required to change the remote URL. To perform this operation, the “$ git remote set-url <remote-url>” command can be utilized.

This post discusses the procedure to change the URL for remote Git repositories to HTTPS and SSH URLs.

Change the Git Remote Repository URI (URL) to HTTPS?

To change the URL for the remote Git repository to the HTTPS URL, firstly, open the GitHub hosting service, and navigate to the remote repository. Then, copy the HTTPS and move to the desired directory where the local repository is located. Lastly, execute the “$ git remote set-url origin <https-url>” command.

Now, let’s move toward the implementation of the above-discussed instructions!

Step 1: Copy HTTPS URL

First, visit GitHub and open the desired remote repository. Then, hit the below-highlighted button and copy the HTTPS URL:

Step 2: Navigate to Directory

Navigate to the desired directory where the local repository is located using the “cd” command:

$ cd "C:\Users\nazma\Git\Demo14"

Step 3: Check Remote List

Next, execute the “git remote” command with the “-v” option to list the existing remotes and view their URLs along with the name:

$ git remote -v

It can be seen that currently, some SSH remote connection exists:

Step 4: Set HTTPS URL

Now, run the “git remote set-url” command with the copied HTTPS URL:

$ git remote set-url https://github.com/GitUser0422/new_repo.git

Step 5: Verify Added Remote

Lastly, execute the provided command to ensure the remote(origin) is added:

$ git remote -v

As you can see, the URL is successfully changed to HTTPS:

Let’s move ahead to the next section to change the URL for the remote Git repository to SSH.

Change the Git Remote Repository URI (URL) to SSH?

To add the SSH URL for the remote Git repository, firstly, open the remote repository. Then, hit the “Code” button and copy the SSH. Move to the desired directory where the local repository is located. Execute the “$ git remote set-url origin <ssh-url>” command to add the SSH URL for a remote Git repository.

Now, follow the below provided instructions to understand the discussed procedure!

Step 1: Copy SSH URL

Open your Git remote repository, hit the “Code” button, and copy the SSH URL:

Step 2: Set SSH URL

Next, run the “git remote set-url” command along with the remote repository name and URL:

$ git remote set-url origin git@github.com:GitUser0422/new_repo.git

Step 3: Verify List Connections

Lastly, run the “git remote” command along with the “-v” option to verify the performed operations:

$ git remote -v

The below output shows that the URL for the remote Git repository is changed:

We have offered the methods to change the URL for remote Git repositories to HTTPS or SSH URLs.

Conclusion

To change the URL for the remote Git repository to HTTPS or SSH URL, firstly, open the GitHub hosting service and go to the desired remote repository. Then, copy the HTTPS or SSH URL and move to the desired directory where the local repository is located. Used the “$ git remote set-url origin <remote-url>” command. This post illustrated the methods for changing the remote Git repository URL to HTTPS or SSH URLs.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.