How to Change a Git Remote?

Learn via video courses
Topics Covered

Overview

In some cases, developers decide to change the name of the remote repository or they may also decide to move the remote repository to another location. So, in such cases, we use the git change remote command to add the new remote URL or in simpler terms to change the Git repository. The .git directory stores the details of the remote server and other such stuff are managed and stored in this folder only. So, whenever we change the URL of the remote repository, the URL details stored in the .git/config file is also changed.

Pre Requisites

The prerequisites for learning the git change remote command can be a basic understanding of Version Control Systems, Branching, and Git. Before learning about the git change remote command, let us discuss them briefly.

A version control system is a tool in software development that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. Apart from that, we must know the concepts of the git remote command and the git remote add command as both of these commands will help us to add the remote repository to our local git repository. Before learning how to change the git repository, we must be familiar with the concept of the remote repository. A repository is a kind of folder or directory that is hosted or stored on the remote server so that it can be accessed by fellow developers working on the repository or the current project. We can compare a Git repository with a file system present in our system, we can similarly compare a Git repository present on the remote server with the remote file system present on a remote system.

One of the most common examples of a remote Git repository can be GitHub. As we know that GitHub is a central repository made to host our local repository and it helps numerous developers to collaborate on a project and help the entire community and organization. On the other hand, a Git working directory is nothing but a directory containing all the files that are tracked by Git. Git will track all the files present in the directory and update if any change happens.

Introduction

Before changing the Git repository and learning about the git change remote command, we must upload our local work to the remote server using the git push command. Once the content is pushed, we can easily change our remote server to another remote server of our choice.

The git push branch command is used to upload our local work and changes to the respective remote branch present on the remote server or remote repository. A one-liner definition of the git push branch command can be - a command that is used to update the remote branch with our local commit(s). We have four main commands in Git that works with the remote repository. The git push branch command is one of those commands. We can even shorten this command if we are using the main or master branch. So, if we are working on the main branch, we can simply use the git push command. We should always watch our status and current working area using the git status command before pushing our local work to the remote repository.

Before learning how to push our local work to the remote repository, let us first see how we push our work to the local repository so that we can use the git push branch command without any issue.

  1. Initialize the local Git repository. The command for the same is:
  1. Add all our necessary local files to the Git repository using the git add command. The command for the same is:
  1. Finally, we need to save our changes to the staging area so that the changes can be directly pushed to the remote repository.

Now before pushing the changes to the remote server, we need to make sure that we have already added the remote repository connection. For adding the remote server, we can use the git remote add "remote-URL" command. We can also check if the remote is already added or not using the git remote -v command.

  1. The final step is to push our local repository to the server using the git push -u origin "branch-name" command. We can even change the name of our master or the main branch using the command - git branch -M main if the current name is master.

Let us now take a scenario to understand the need for the git change remote command. Suppose you are working with a huge team of developers and you have cloned the entire repository (project) onto your system. Suppose you have made some changes and committed those changes in the local Git repository. Now in some cases, developers decide to change the name of the remote repository or they may also decide to move the remote repository to another location. Now, in both the above cases, the previous URL of the remote server (that we have added using the command: git add origin "URL") has changed, and hence the reference to the new remote repository has changed. So, in such cases, we use the git change remote command to add the new remote URL or in simpler terms to change the Git repository.

Whenever we change the URL of the remote repository, the URL details stored in the .git/config file are also changed. The .git directory stores the details of the remote server and other such stuff are managed and stored in this folder only.

Another simpler way of changing the remote URL or performing the git change remotely is to clone the remote repository again with the new URL of the remote repository

What is a Git Remote?

Let us now learn what is a Git remote and how the git change remote command works. A Git remote is nothing but a pointer. The main aim of the Git remote pointer is to point to the link between the local repository version and the remote repository version.

As we know that Git and GitHub are used by numerous developers to connect and share their work on the certain project(s). Now as the developers clone the remote repository to the local system using Git. In the case of large projects, there may be more than one remote added. But usually, we only have one remote repository added to the local system.

So a question that may come to our mind is - What is the need for these many remotes if we can work with only one remote and why do we even use the git change remote command as we can work with a single remote? Well, in large projects, there are numerous phases of development such as development, testing, deployment, maintenance, etc. So, for working with the various development environments or phases like testing, deployment, maintenance, etc., we use various remotes. This feature helps us to separate our development work and other associated tasks so that one must not be hampered by the other.

Please refer to the image provided below for more clarity. Git Remote

The git change remote command is used when we change the name of the remote repository or when we move from one hosting platform to another like GitHub to GitLab.

We can also list all the available remotes using the git remote -v command.

Change Git Remote URL

When we open the remote repository, we can see the cloning option has three options:

  1. Using the HTTP protocol.
  2. Using the SSH protocol.
  3. By downloading the zip file of the remote repository.

Let us first see how we can change the URL of the remote repository using the HTTP protocol. We can use the git remote set-URL command to change the remote repository. The git remote set-URL origin _:_ command takes two arguments:

  1. The name of the existing remote URL (generally it is referred to as origin).
  2. The name of the new (to be added) remote URL.

Let us assume that the previous URL of the Git remote repository is: git@your.git.repo.example.com:user/repository.git. And we want to change its URL to : git@your.git.repo.example.com:user/repository2.git.

The overall command for the same is:

Now, if we try to use the link that does not exist or the link that does not belong to any remote repository then we will get the error saying:

Change a Remote Manually

We can manually change the remote repository link (perform the same task as the git change remote command) by making changes to the configuration of the Git repository. This approach is more practical when we make several changes to the Git configuration file in the current repository. Ri make changes to the configuration file, we first need to open the .git/config file and then move to the section containing: [remote "origin"] then we can change the old URL with the new URL of the remote repository.

We should not opt for his approach as a small irrelevant change to the Git config file may result in a lot of data loss and other problems. So, if you have a good understanding of Git then you can opt for this step.

Changing Git Remote to SSH

Let us now see how we can change the Remote repository URL using the SSH protocol.

Similar to the HTTP approach, if we want to change the SSH URL, we can use the git remote set-URL command.

The git remote set-URL origin _:_ command takes two arguments:

  1. The name of the existing remote URL (generally it is referred to as origin).
  2. The name of the new (to be added) remote URL.

Let us assume that the previous URL of the Git remote repository is: git@your.git.repo.example.com:user/repository.git. And we want to change its URL to : git@your.git.repo.example.com:user/repository2.git.

The overall command for the same is:

Conclusion

  • Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc.
  • In some cases, developers decide to change the name of the remote repository or they may also decide to move the remote repository to another location. In both the above cases, the previous URL of the remote server has changed, and hence the reference to the new remote repository has changed.
  • So, in such cases, we use the git change remote command to add the new remote URL or in simpler terms to change the git repository.
  • The .git directory stores the details of the remote server and other such stuff are managed and stored in this folder only. Whenever we change the URL of the remote repository, the URL details stored in the .git/config file are also changed.
  • A Git remote is nothing but a pointer. The main aim of the Git remote pointer is to point to the link between the local repository version and the remote repository version.
  • In large projects, there are numerous phases of development such as development, testing, deployment, maintenance, etc. So, for working with various development environments, we use various remotes. Git changes remote feature helps us to separate our development work and other associated tasks so that one must not be hampered by the other.
  • We can manually change the remote repository link by making changes to the configuration of the Git repository. This approach is more practical when we make several changes to the Git configuration file in the current repository.
  • We can change the remote using the SSH protocol URL as well as the HTTP protocol URL using the command: git remote set-URL origin _:_.
  • To make changes to the configuration file, we first need to open the .git/config file and then move to the section containing: [remote "origin"] then we can change the old URL with the new URL of the remote repository.
  • We should not opt for his approach as a small irrelevant change to the Git config file may result in a lot of data loss and other problems. So, if you have a good understanding of Git then you can opt for this step.