Changing the Remote Origin in Git

Changing the remote origin in Git is a common task that can be essential for managing your source code effectively. We explore how to easily do it.

April 30, 20244 min read

In Git, the term "remote origin" refers to the default upstream repository that is associated with your local repository. It’s where your code is stored remotely, and it plays a crucial role in collaboration and version control in software development. There may be several reasons why you would need to change the remote origin URL of your Git repository. This tutorial will guide you through how to change the remote origin, explain why you might need to do it, and address common troubleshooting issues you might encounter.

How to Change the Remote Origin URL

Changing the remote origin URL in Git is a straightforward process that involves a few commands in your terminal or command prompt. Here are the steps:

1. Open Your Terminal

Start by opening your terminal or command prompt.

2. Navigate to Your Repository

Navigate to the directory of your local Git repository using the cd command.


cd path/to/your/repository

3. Check Existing Remotes

Before you change the remote origin, check the existing remote setup with git remote -v. This command lists the remote connections you have to other repositories. Typically, you will see something like this:

origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

4. Change the Remote URL

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

git remote set-url origin https://github.com/user/newrepo.git

After changing the URL, verify the change by listing the remotes again:

git remote -v

You should see the URL updated to the new remote origin.

Reasons You Might Need to Change the Remote Origin

  • Project Migration: You might be moving your project to a different repository host (from GitHub to GitLab, for example) or to a different account.
  • Repository Ownership Changes: The original repository might have changed ownership, and you need to update the remote URL to reflect this.
  • Branching Strategies: In some cases, especially in complex projects, you might want to point to a different fork or a different branch temporarily.
  • Correction of Mistakes: You might have initialized your repository with the wrong remote URL.

Try Kodaschool for free

Click below to sign up and get access to free web, android and iOs challenges.

Sign Up

Troubleshooting Common Issues

When changing remote origins, you might encounter some issues. Here are a few common ones and how to resolve them:

Issue 1: Permission Denied

If you receive a permission denied error after changing the remote origin, it’s likely that you don’t have the correct access rights to the new repository. Ensure you have the right permissions, and that you are using the correct authentication method (SSH keys, username/password, etc.).

Issue 2: Repository Not Found

This error occurs if the URL is incorrect or the repository does not exist. Double-check the URL for any typos and verify that the repository exists at that URL.

Issue 3: Branch Mismatch

After changing the remote origin, you might find that your branches do not line up with those in the new repository. Use git fetch to update the branch information from the new repository and check the branches using git branch -a.

Solving the "Remote Origin Already Exists" Error

If you encounter the "remote origin already exists" error while changing the remote origin in Git, there are several potential steps to resolve it.

1. Remove the Existing Remote

If there's already a remote called origin but it's incorrect, you can remove it and add the correct one. This scenario typically arises when switching between hosting platforms like GitHub and GitLab. Here's how:

git remote remove origin

2. Update the Existing Remote's URL

Instead of removing and re-adding the remote, you can simply update its URL with a single command:

git remote set-url origin <NEW-URL>

3. Rename the Existing Remote

If you need to keep the old remote while adding a new one, you can rename the existing remote and then add the new one:

git remote rename <old-name> <new-name>

For example, you could rename your origin remote to new-origin like this:

git remote rename origin new-origin

4. Additional Solutions

There are other potential solutions to explore based on your specific situation. These include checking for typos in the remote URL, verifying your Git configuration, and ensuring proper authentication.

Myles Mburu

About Myles Mburu

Software Developer | AWS Solutions Architect