How to Change Remote Origin URL in Git

Change Remote Origin URL in Git

Imagine you are working on a project hosted on GitHub. Your organization migrates all repositories to GitLab for better integration with its CI/CD tools. As part of this migration, you need to change the remote origin URL in your local Git repository to point to the new GitLab repository.

In this tutorial, you’ll learn how to change the remote origin URL of your Git repository.

We’ve got a lot of valuable insights to explore, so let’s dive right in!

What is a Remote Origin?

Before diving into the steps to change the remote origin, it’s essential to understand what a remote origin is. In Git, a remote is a shared repository all team members use to exchange their changes. The term “origin” is a shorthand name for the default remote repository. When you clone a repository, the remote URL is stored with the name “origin” by default.

For example, when you clone a repository from GitHub, the remote URL is automatically saved under the name “origin.” This allows you to interact with the remote repository using simple commands like git push and git pull, which will push your changes to or pull updates from this “origin.”

Why Change the Remote Origin?

There are various reasons you might need to change the remote origin URL:

  • Migrating to a new Git hosting service: For example, moving from GitHub to GitLab.
  • Updating the repository URL: If the remote repository’s URL has changed.
  • Switching to a different repository: If you use a different repository for your project.

View Your Current Remote Origin URL

Before changing the remote origin, it’s a good idea to check the current remote URL to ensure you make the correct changes.

First, navigate to your git repository.

 # cd your-repo

Then, check your current remote URL.

 # git remote -v

This command will display the URLs that Git uses to fetch and push changes.

origin  https://github.com/username/your-repo.git (fetch)
origin  https://github.com/username/your-repo.git (push)

The output lists the remote names (origin) and the corresponding URLs for fetching and pushing changes. By default, both fetch and push URLs are the same.

Method 1: Change the Remote Origin Using an HTTPS URL

You can use the git remote set-url command followed by origin name and new URL to change your remote origin. Follow the below steps to update the remote origin using HTTPS.

Step 1: Remove the Existing Remote (Optional)

If you prefer, you can remove the existing remote origin before adding a new one. This step is optional but can help avoid confusion if multiple remotes are set up.

 # git remote remove origin

This command removes the current origin from your repository. If you recheck your remotes with git remote -v, you’ll notice that the origin is no longer listed.

Step 2: Add the New HTTPS Remote URL

Now, you can add the new remote URL. This URL will typically be the new location of your repository on the new Git hosting service.

 # git remote add origin https://gitlab.com/username/your-repo.git

Step 3: Verify the New Remote URL

You should verify the remotes again to ensure that the new remote URL has been added correctly.

 # git remote -v

Output.

origin  https://gitlab.com/username/your-repo.git (fetch)
origin  https://gitlab.com/username/your-repo.git (push)

Step 4: Directly Update the Remote Origin URL

Instead of removing and re-adding the origin, you can directly change the URL with the git remote set-url command:

 # git remote set-url origin https://gitlab.com/username/your-repo.git

This command updates the URL associated with the origin without removing it first, making it a quicker option if you’re confident in the change.

Method 2: Change the Remote Origin Using an SSH URL

If your organization prefers using SSH for added security, you must change the remote origin URL to an SSH URL.

Step 1: Remove the Existing Remote (Optional)

As with the HTTPS method, you can remove the existing origin first, although it’s unnecessary.

 # git remote remove origin

Step 2: Add the New SSH Remote URL

SSH URLs are more secure and are commonly used in environments where you frequently interact with private repositories.

 # git remote add origin [email protected]:username/your-repo.git

Step 3: Verify the New Remote URL

Verify that the SSH URL has been added correctly:

 # git remote -v

Output.

origin  [email protected]:username/your-repo.git (fetch)
origin  [email protected]:username/your-repo.git (push)

Step 4: Directly Set the SSH Remote Origin URL

As with HTTPS, you can directly update the SSH URL without removing the existing origin:

 # git remote set-url origin [email protected]:username/your-repo.git

Step 5: Generate and Add SSH Keys (If Required)

If you haven’t already set up SSH keys, you’ll need to do so. SSH keys authenticate your machine to the remote server without requiring a password.

Generate an SSH key:

 # ssh-keygen -t rsa -b 4096 -C "[email protected]"

Add the SSH key to your SSH agent:

 # eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_rsa

Finally, add your SSH key to your Git hosting service. For example, in GitLab:

  • Go to your GitLab account settings, navigate to “SSH Keys,” and add your new SSH key.

Conclusion

In this article, you learned how to change the remote origin URL of a Git repository. You now understand the reasons for changing a remote origin, how to view your current remote URLs, and the steps to update to a new remote URL. Remember to verify your changes and ensure that you have the necessary authentication set up for the new remote.

Keep exploring more Git tips and best practices on our blog to stay ahead in your development journey!

FAQs

1. What happens if I don't change the remote origin URL after migrating to a new Git hosting service?

If you don’t update the remote origin URL, Git will continue to push and pull from the old repository. This could result in errors or outdated changes.

2. Can I have multiple remote origins in a single Git repository?

Yes, you can set multiple remotes in a single Git repository. Each remote can have its own name. They allows you to push or pull from different repositories.

3. How do I remove a remote origin completely from my repository?

You can run git remote remove origin to remove a remote origin.

4. Can I switch between HTTPS and SSH URLs for the same repository?

Yes, you can switch between HTTPS and SSH by updating the remote URL using the git remote set-url command.

5. Is SSH more secure than HTTPS for Git operations?

SSH is generally considered more secure than HTTPS because it uses encrypted keys instead of passwords, reducing the risk of credential exposure.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva