How to update the URL for a remote Git repository

How to update the URL for a remote Git repository

Introduction

There may be times when you need to update the URL for a remote Git repository. For example, you may have moved your remote repository to a different server, or you may want to push to a different remote repository. Most of the time I need to do this because I am meddling with repository names after I have created them (don’t do this, be less Justin). This post shows you how to update the URL for a remote Git repository.

I have renamed a repository from data-ops to data-ops-demos and I want to update the URL to reflect this change. Note here, I can do this because I am the only one using this repository. If you’re working with others, don’t just flagrantly change the name of the repository because your fellow developers will be very unhappy with you.

Check current URL

The easiest way to check the current URL for a remote Git repository is to use the git remote -v command. This command lists the names of the remote repositories and their URLs. Navigating to my local repository in terminal and running that command returns the following:

origin  https://github.com/justinjbird/data-ops.git (fetch)
origin  https://github.com/justinjbird/data-ops.git (push)

The two entries represent where my repository will fetch from and push to. The URL is the same for both. In theory there can be more than one remote repository, but I am not going to cover that here.

Update the URL

You could edit the .git/config file in your local repository and change the URL for the remote repository. However, it’s much easier to use the git remote set-url command. This command changes the URL for the remote repository. The syntax for the command is git remote set-url origin {new-url}. So I will need to run:

git remote set-url origin https://github.com/justinjbird/data-ops-demos.git

You don’t get any feedback from this command, but re-running git remote -v will show the updated URL:

origin  https://github.com/justinjbird/data-ops-demos.git (fetch)
origin  https://github.com/justinjbird/data-ops-demos.git (push)

Now I can now push to my remote repository again!

Conclusion

Whilst this isn’t something you should be doing all the time, it’s good to know how to update the URL for a remote Git repository. It’s a simple command that can save you a lot of time and hassle.

Tags :

#mtfbwy



Recent Posts

How to Search for a Lost File in the Git Log

How to Search for a Lost File in the Git Log

  • 2024-04-27
  • 4 minutes to read

I have lost a file in my Git repository. How can I find it by searching the git log?

Read More
No Such Shell Function 'Zle Line Init' in Zsh

No Such Shell Function 'Zle Line Init' in Zsh

  • 2024-04-25
  • 3 minutes to read

Troubleshooting the error message "no such shell function 'zle line init'" in zsh when using OhMyPosh.

Read More
Getting Started With Python in Vscode

Getting Started With Python in Vscode

  • 2024-04-05
  • 2 minutes to read

This post will help you get started with Python in Vscode and identify some of the useful extensions to install.

Read More