How to Add Remote Repository in Git?

Learn via video courses
Topics Covered

SVN relies on a centralized repository for developer collaboration, exchanging changesets between working copies and the central hub. In contrast, Git adopts a distributed model, providing each developer with a separate repository containing local history and branches. Git facilitates sharing entire branches between repositories, and the git remote command, a crucial component, registers records for syncing changes using git fetch, git push, and git pull commands. Know more about Git Remote.

How to Add Remote Repository in Git?

To add a remote repository in Git, you have to provide the mapping of the remote repository present on GitHub to the default name of remote origin via the protocol HTTPS/URL using the following command:

command-to-connect-a-local-repository-to-github

This will successfully connect and configure your local repo to GitHub. Now, to verify all the existing remotes, you can use the command:

command-to-verify-the-existing-remotes

It will fetch a list of all the existing remotes in git. There might also be the case when you use the git remote add origin <remote_repo_url> to add a remote repository and you get an error something like this:

This error usually occurs when you have an already existing remote with the same name. To resolve this, you can either provide a new different name for the new remote repository or you can change the name of an existing git repo and then add a new remote repository. It can also be avoided by deleting the existing remote repository if it's not in use anymore and then you can add the new remote. Now, you can easily push your code changes into your remote repo on GitHub by running the git push <remote> <branch> command.

To get the URL of the remote repository, go to the main page of the repository, and copy the HTTPS or SSH network protocol URL to clone the repository as shown in the image below:

option-to-select-http-or-ssh-network-url-to-clone-repo

Change an Existing Remote Repository URL

To change a URL for an existing remote repository, you can first check the above section on how to add a remote repository in git. Then, you can use the git remote set-url command. In this command, you have to pass the name of the remote you want to map and the URL of the remote repo. In the remote name, you can choose origin or upstream and for a new URL for the remote, you can either use the HTTPS or SSH URL.

If you're changing the URL to use HTTPS, then your new URL might look like this:

If you're changing the URL to use HTTPS, then your new URL might look like this:

So, you can easily add these parameters to the above remote set URL command:

Switching Remote URLs from SSH to HTTPS

To switch a remote repo URL from SSH to HTTPS, you can use the same above command and provide the new HTTPS URL.

Here we put the new "HTTPS" URL in the set URL command as the new URL we want. You can confirm that the remote URL has changed, by using:

As you can see now the output has HTTPS remote URLs instead of SSH URLs.

You can do the same thing in the case of switching a remote URL from HTTPS to SSH. Let's first check the status of the remote URLs with the:

The output of the above existing remote URLs is in form of an HTTPS URL:

Now, we can switch the URL to SSH, using the command:

You can Verify that the remote URL has changed to the SSH URL.

Output:

Rename an Existing Remote

To rename an existing remote repository, you can use the git remote rename command. This command accepts two values, an existing remote name, and a new remote name. First, let's check for existing remotes with the command:

Output:

command-to-verify-the-existing-remotes2

As we can see the existing remote name is origin and let's say we want to change it to the new remote name as New_remote. Then, you can use the command and verify the new remote name by using the git remote -v command:

output-command-to-rename-an-existing-remote

We can see from the output above, the remote name has been updated to New_remote from origin.

Remove a Remote Url from Your Repository

To remove a remote URL from your repository you can use this command:

This will remove the remote URL from the repository and detach the local and remote repositories. It will not delete the remote repository instead. Let's say you originally had these current remotes as shown in the image below:

command-to-verify-the-existing-remotes3

Now, we want to delete this remote named destination, then we will type the command:

command-to-remove-remote-url-from-repository

Note: The git remote rm command will work on removing and detaching the remote and its references from your local repository. It will not remove the repository from the cloud based version control server.

Use the Git Remote Add Origin Command to Push Remotely

As seen above, you can connect your local repository to GitHub using the git remote add origin command. Now, to push all the code changes and their history to a remote server like GitHub, you can use the git push command. For that, you will need to add the --set-upstream option to the push command. Otherwise, the push action will be denied by the remote server. You must also specify the name of the branch to push the code changes. In this case, it is the master branch on which we want to push the changes.

This action in git will work on pushing the changes and it will list down all the objects pushed to the server along with the count and will state that your local repository is set to track to a branch named as master on the remote server like GitHub. To confirm that the changes were pushed to the repository, you should be able to navigate to the remote repository on GitHub and see the changes to the files you pushed

Conclusion

  • Git is the most widely used free and open source distributed version control system used to efficiently handle small to very large projects.
  • It is used to track the changes in the source code and projects across different teams and revision levels. Git enables multiple developers to work together on a non-linear parallel development basis.
  • Remote repositories are the remote versions of your project locally, and these are hosted on the cloud-based platform on the Internet. We saw the concept of how to add a remote repository in git.
  • You can use the git remote add origin <remote_repo_url> to connect your local repository to Github.
  • To push all your code changes to the remote server and to track the history you can use the git push command along with the --set-upstream and the name of the branch you want to push the changes to.
  • To remove a remote URL from your repository you can use the git remote rm <remote_name> command.
  • To rename an existing remote repository on the server you can use the git remote rename <old_repo_name> <new_repo_name>
  • To change a URL for an existing remote repository, you can use the git remote set-URL <new_url> command.
  • To add a remote repository in git, you have to provide the mapping of the remote repository on GitHub with the name of the default remote origin via the protocol HTTPS/URL using the git remote add origin <remote_repository_url> command.
  • The git remote -v command is used to fetch and list down all the existing remotes.
  • The git remote rm command is used to detach and remove the remote repo and its references from your local repository. It does not remove the repository from your remote server.
  • You can initialize the Git repository for the local project repository with the git init command. This will initialize the repository and will keep all the files of your project together with a special folder called .git.