How to update the remote URL of a Git repository
- version control
- 2024-02-24
- 4 minutes to read
- git
Table of Contents
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 are 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!
What if there is no response to git remote -v?
If you don’t get a response when you run git remote -v
, it’s likely that the repository has never had a remote repository set. In this case, you simply need to run the command:
git remote add origin https://github.com/justinjbird/data-ops-demos.git
This will add the remote repository to your local repository and you can now push to it.
What if I want to add a second remote repository?
This is a slightly more obscure use case, but I have a single repository that I am starting to build out that is looking at behaviours in Github and Azure DevOps, so I want to push one single repository to both locations. You can add a second remote repository by running the add command:
git remote add secondary https://[email protected]/sandbox/DataOps/_git/DataOps
This will add a second remote repository url to your local branch referred to as “secondary” which I can confirm by running git remote -v
:
git remote -v
Here is the output:
origin https://github.com/justinjbird/data-ops-demos.git (fetch)
origin https://github.com/justinjbird/data-ops-demos.git (push)
secondary https://[email protected]/sandbox/DataOps/_git/DataOps (fetch)
secondary https://[email protected]/sandbox/DataOps/_git/DataOps (push)
Now I can push to secondary location by running the command git push {name} {branch}
:
git push secondary main
Here is the output:
Enumerating objects: 47, done.
Counting objects: 100% (47/47), done.
Delta compression using up to 10 threads
Compressing objects: 100% (38/38), done.
Writing objects: 100% (47/47), 6.26 KiB | 1.04 MiB/s, done.
Total 47 (delta 17), reused 3 (delta 0), pack-reused 0 (from 0)
remote: Analyzing objects... (47/47) (5 ms)
remote: Validating commits... (9/9) done (0 ms)
remote: Storing packfile... done (68 ms)
remote: Storing index... done (54 ms)
To https://[email protected]/sandbox/DataOps/_git/DataOps
* [new branch] main -> main
This results in a more convoluted process to commit code. Running the command git push
will push to origin only, so pushing to both locations requires you to run git push twice (by also running git push secondary {branch name}
). One way around this would be to create a git alias that pushes to both locations in one command.
Wrapping Up
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.