Change Git Origin Remote URL

Last Updated on 2023-05-03

In this post, we will deal with changing the remote git repository associated with a local git-enabled directory.

If you are here and don’t yet have a git repository set up, read about creating a git repository to get started, and then pushing a local repository to a remote origin. At that point, you will have your upstream set and can proceed with the following command to update the origin:

 git remote set-url origin <url>

[#recall-syntax]Spare yourself remembering the syntax with a Warp Workflow to do this[#recall-syntax]

If you are using Warp as your terminal and you want to quickly retrieve the command to change the remote url of your local git repository, you can use Warp’s Workflow feature by pressing [.inline-code]CTRL-SHIFT-[.inline-code]`, typing [.inline-code]change url of remote git repository[.inline-code], then pressing [.inline-code]ENTER[.inline-code] to use the suggested command:

All you then have to do is enter the new desired remote url and it should be changed!

[#reasons-to-change]Good reasons to consider changing your remote URL[#reasons-to-change]

Here are some examples of situations where changing your remote URL makes sense:

  1. Switching from HTTP to SSH: This can provide better security and authentication as well as fast connection speeds. This should not affect the state of the remote repository or impact the interaction with your co-workers but can make your work more secure. To learn more about using Git with SSH, you can read our post on how to clone, push, and pull on a remote git repository with ssh.
  2. Moving to a new hosting provider: Such as from GitHub to GitLab will require a change of URL. This is likely to affect your coworkers as they will also have to change their remote URL.
  3. Renaming your repository: This will require a URL change to reflect the new repository, which all your coworkers will also need to change.
  4. Updating credentials: If your authentication details have changed (e.g. password) then you may need to update the URL to reflect that. In some cases however this may only affect your local credentials, not the URL.

Generally, changing the URL of a remote repository is a relatively simple and harmless process, but it is important to ensure that all team members are aware of the change to avoid any disruptions in workflow. 

[#connect-to-remote-repo]Connecting to a remote repository[#connect-to-remote-repo]

To connect a local git-enabled repository to a remote repository, you can use the following command:

 $ git remote add <name> <URL>

Where [.inline-code]name[.inline-code] is an alias for the remote repository that can be used to interact more easily with it. This includes [.inline-code]git push[.inline-code] or [.inline-code]git pull[.inline-code], where you can use 'name' instead of the repository's URL. The most commonly used one is [.inline-code]origin[.inline-code].

And [.inline-code]URL[.inline-code] is the URL of the remote repository that often takes the form [.inline-code]git@<host>:<username>/<repository>.git[.inline-code]. For example: [.inline-code][.inline-code].

To verify that your local repository has successfully been connected to a remote repository, you can use the following command:

 $ git remote -v

Which should print the remote URL the local repository is connected to.

[#change-remote-repo-url]Changing a remote repository URL[#change-remote-repo-url]

A common use case for changing a remote repository URL is when you want to switch from HTTPS to an SSH connection as it is considered more secure and efficient, especially for repositories that you need to interact with frequently.

To change the remote URL of a connected Git repository you can use the following command:

 $ git remote set-url origin <new_url>

Where [.inline-code]origin[.inline-code] is the name of the remote repository you want to modify and [.inline-code]new_url[.inline-code] is the new URL you want to connect the Git repository to.

To check that the new remote has been successfully set, you can once again use the following command:

 $ git remote -v