Data report"State of code review 2024" is now liveRead the full report

How to change the remote URL in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Changing the remote URL in Git is necessary when you need to update the location of the repository your local project is linked to. This could be due to a change in the repository's hosting service, a move to a different URL, or a need to switch from HTTPS to SSH for security reasons. This guide will walk you through the steps to change the remote URL in Git, ensuring your local repository points to the correct remote server.

A remote in Git is a common repository that all team members use to exchange their changes. In most cases, the remote repository is stored on a server like GitHub, GitLab, or Bitbucket. The URL of this remote is what you need to set up or change to push to and pull from the remote repository.

Before changing the remote URL, it's a good idea to check what the current remote URL is. This can be done using the git remote -v command, which lists all current remotes associated with the repository and their URLs.

Terminal
git remote -v

This command will show something like:

Terminal
origin https://github.com/username/repository.git (fetch)
origin https://github.com/username/repository.git (push)

To change the remote URL, use the git remote set-url command, followed by the name of the remote (typically origin), and then the new URL.

Terminal
git remote set-url origin https://newurl.com/repository.git

Replace https://newurl.com/repository.git with the actual URL of the new remote repository.

After updating the remote URL, it's important to verify that the change was successful. Use the git remote -v command again to check that the remote URL has been updated.

Terminal
git remote -v

The output should reflect the new URL:

Terminal
origin https://newurl.com/repository.git (fetch)
origin https://newurl.com/repository.git (push)
  • Switching from HTTPS to SSH: If you are changing from an HTTPS URL to an SSH URL, the command might look like this:

    Terminal
    git remote set-url origin git@newurl.com:username/repository.git

    This switch is often made for security reasons or to avoid entering credentials frequently.

  • Troubleshooting: If you encounter issues after changing the remote URL, ensure you have the correct permissions to access the new repository and that you have no network issues.

  • Updating submodules: If your repository contains submodules, you may also need to update the URLs in your .gitmodules file and sync the submodules. For more details, see this guide on updating Git submodules.

  • Backup before making changes: It's a good practice to ensure that all your local changes are backed up or correctly pushed to the remote before making changes to the remote URL.
  • Consistency across team: If you're working in a team, communicate the change and ensure that all members update the remote URL in their local repositories to avoid confusion or errors in collaboration.
  • Use clear remote names: If you work with multiple remotes, use clear and descriptive names for each remote instead of the generic origin to avoid confusion.

For further reading on changing Git remote URLs see the official Git documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2