How to Change Remote Origin in Git

Knowing how to change a remote origin in Git allows you to adapt to different scenarios and requirements in your development workflow.

Imagine you've been hosting your project on GitHub, enjoying its familiar interface and tools. However, in a hypothetical scenario, GitHub experiences a temporary service outage, making it challenging to access your project when you need it the most. While such outages are rare, they can disrupt your workflow. To ensure uninterrupted development, you decide to migrate your project to a different hosting service, such as GitLab. Right here is where the basic skill of migrating your remote repository from one host to another using Git comes in handy.

In this guide, we will walk you through the process of changing the remote origin in Git, enabling you to transition between remote repositories easily.

Key Takeaways 

  • Git offers the flexibility to change the remote origin of your code repository, allowing you to seamlessly transition between different hosting services.
  • Git offers both HTTPS and SSH URL options for setting up the new remote origin.
  • Changing remote origins in Git is generally safe, and it doesn't impact your existing codebase or commit history.

What is Git?  

Git is a distributed version control system (DVCS), created by Linus Torvalds to help developers collaborate and track changes in their source code. Git is used in various software development workflows and has become an essential tool for managing code. To learn more about Git, check out this blog post: DevOps: Git for Beginners!

However, to harness the full potential of Git, you need to know how to manage your remote repositories effectively. One essential skill is changing the remote origin, which allows you to connect your local Git repository to a different remote repository, such as moving from GitHub to GitLab.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  • Git installed on your computer.
  • A Git repository with an existing remote origin.
  • A GitHub and GitLab account.

Try the Git remote repository Lab for free

Git remote repository Lab
Git remote repository Lab

Open a Terminal/Command Prompt

To start the process, you'll need to open a terminal or command prompt on your computer. This is where you'll enter the Git commands to make the necessary changes.

Use the cd command to navigate to the local directory of your Git repository. Replace ‘C:\Users\KodeKloud\sample’ with the actual file path to your repository on your system.

cd C:\Users\KodeKloud\sample

Check the Current Remote Origin

Before making any changes, it's essential to know the current remote origin URL. You can do this by running the following Git command:

git remote -v

This command will display the current remote URL for both fetch and push operations. For example, if your project is hosted on GitHub, you might see an output like this:

origin	https://github.com/KodeKloud/repository-1.git (fetch)
origin	https://github.com/KodeKloud/repository-1.git (push)

Change the Remote Origin

To change the remote origin URL, use the git remote set-url command, followed by the remote name (usually "origin") and the new URL. Depending on your preferences and access to the remote repository, you can choose between using an HTTPS URL or an SSH URL. Don’t worry, we'll cover both options.

  1. Using an HTTPS URL:

For example, if you want to change the origin URL to a new GitLab repository, use the command:  

git remote set-url origin https://gitlab.com/KodeKloud/repository-1.git

Simply replace “https://gitlab.com/KodeKloud/repository-1.git” with the URL of the new remote GitLab repository you want to connect to.

  1. Using an SSH URL:

If you prefer to use SSH, you can update the remote origin URL to use SSH by using the following command:

git remote set-url origin [email protected]:KodeKloud/repository-1.git

Replace “[email protected]:KodeKloud/repository-1.git” with the SSH URL of the new remote repository.

Please note that you need to have your SSH key properly set up and associated with your GitHub account for SSH-based authentication to work.

Verify the Change

To ensure that the remote URL has been updated successfully, use the git remote -v command again. It should now display the new URL for both fetch and push operations. For example:

For HTTPS URL:

origin	https://gitlab.com/KodeKloud/repository-1.git (fetch)
origin	https://gitlab.com/KodeKloud/repository-1.git (push)

For SSH URL:

origin	[email protected]:KodeKloud/repository-1.git (fetch)
origin	[email protected]:KodeKloud/repository-1.git (push)

The output will show the updated remote URL, confirming that the remote origin URL has been successfully changed to the new one you specified.

To learn how to delete local and remote Git branches, check out our blog post: How to Delete Local and Remote Branches in Git

FAQs

Q1: Why would I need to change the remote origin?

There are several reasons to change the remote origin in Git, including migrating to a different hosting service, switching between remote repositories, updating repository URLs, collaborating on forks, and more. Changing the remote origin allows you to connect your local repository to a new remote repository.

Q2: Is it safe to change the remote origin?

Yes, changing the remote origin in Git is a safe operation when done correctly. However, it's essential to be cautious and double-check the new URL to avoid unintentional changes. Communicate the change with your collaborators if you're working on a shared project.

Q3: What happens to my existing branches when I change the remote origin?

Your existing branches remain intact when you change the remote origin. However, you'll need to update your local branches to track the new remote repository by using the `git branch --set-upstream-to` command or the `-u` flag when pushing changes.

Q4: Can I change the remote origin back to its original URL?

Yes, you can change the remote origin URL back to its original URL at any time by following the same process outlined in this guide. Git allows you to switch between remote repositories as needed.

Q5: Is there any impact on the commit history when changing the remote origin?

Changing the remote origin does not affect your commit history. The commit history remains unchanged, and you can continue working with the same codebase while interacting with a different remote repository.

Conclusion

Knowing how to change a remote origin in Git allows you to adapt to different scenarios and requirements in your development workflow. Whether you choose to use HTTPS for its simplicity or opt for SSH to enjoy enhanced security and convenience, this guide enables you to confidently manage your Git repositories, ensuring you maintain control over your software projects.

Enroll in our Git for Beginners course to learn and practice more Git concepts.