Git
git remote

git remote command: add, list, remove, change name url, protocol

In this article we'll cover how to add, view, and delete remote repositories, as well as change their names and URLs, using the git remote command. While git is great on its own, it's when you use it with a group of people that it really comes into its own. Let's take a look at the git remote commands to get you started.

1. git remote add: Add/register/connect a new remote repo

When you first create a local repository using the git init command, you won't have any remote repositories connected. If you run the command below, which shows a list of all connected remote repos, there will be no output.

$ git remote
 

To share your source code in your local environment with other team members, you need to connect to a remote repo. A remote repo can be a server that you configure yourself, or it can use a service such as Github or Bitbucket.

Either way, and regardless of the communication protocol you use, the remote repo will have an address ending in .git. Use that address in the command below to add a new remote repo. It's an old convention to add a remote repo named origin if it's the one that serves as your main repo.

So type the following command

$ git remote add origin [url]

Here's the actual execution result

git remote add execution result

The git remote add command completes without any output. If you verify with the git remote command after entering it, you will see the name origin in the list.

A common option used with the add command is -f. This option will automatically fetch data from all branches of the newly added repo after adding the remote repo.

If you get the error message remote origin already exists, it means that there is already a remote repo associated with the name origin, so please check it and remove the rename hook. You can see how to do this in the sections below.

2. git remote -v: Check remote repo name and URL

The git remote command has a -v (--verbose) option. This option allows you to print more verbose information in the list of connected remote repos.

$ git remote -v

This is what the command looks like after adding another remote repo.

git remote -v execution result

Along with the name in the list, the URL is also printed. You can also see that the same name is printed twice for both fetch and push, which means that in some cases the URLs for the two uses are likely to be different. The main reasons for using different addresses are: making a particular repo read-only, blocking the push address, or using different communication protocols.

We'll see how to set each address separately in Section 5 below.

3. git remote remove: Delete a remote repo

Here's how to remove the connection to the remote repo you set up. Just run the following command.

$ git remote remove [name]

or

$ git remote rm [name]

Deleting a remote repo will also remove all branches and settings associated with it. The execution screen will look like this

git remote remove execution result

4. git remote rename: Rename the remote repo

Next, you can change the name you gave to the remote repo. You can change it by running the following command.

$ git remote rename [old_name] [new_name]

When you rename a remote repo, all the branches and settings associated with it are updated as well. Let's see the execution screen

git remote rename execution result

The name origin has been changed to onion.

5. git remote set-url: Change the remote repo URL [#5].

Here is a command to change the URL set for a specific name. Use the following command.

$ git remote set-url [name] [new_url]

This command changes both fetch and push addresses by default; however, if you give it the --push option, you can only change the push URL.

$ git remote set-url --push [name] [new_push_url]

Once you change the PUSH URL, commands without the --push option will only change the FETCH URL address. Check the execution result to see how the URL address was changed.

git remote set-url execution result

6. Introducing the 4 protocols used with remote repos

Git supports a number of protocols for communicating with remote repos. Let's look at four of them.

6.1. local

If a git repo is on the same system, you can access it through the file system. This is mainly useful for testing or local backups.

An example address might look like this

$ git remote add local_repo file:///path/to/local/repo.git

6.2. HTTP/HTTPS

HTTP(S) is a popular communication protocol for Git hosting services because it is the most common and requires no configuration. It also works well with settings such as firewalls and proxies, and especially in the case of HTTPS, has the added benefit of data security through support for encryption.

An example address might look like this

$ git remote add remote_repo https://example.com/user/repo.git

6.3. SSH

SSH is another popular communication protocol because it supports public/private key encryption and can be faster than HTTP(S). Setting it up for the first time in Git is more complicated than HTTP(S), but it can provide performance benefits for large repositories. For this reason, it is supported by most Git hosting services.

An example address might look like this

$ git remote add remote_repo ssh://user@example.com:/path/to/repo.git

6.4 Git

Git supports its own Git protocol over TCP. This protocol does not require user authentication or encryption, so it is often used for read-only implementations in most open source projects, including Git.

An example address might look like this

$ git remote add remote_repo git://user@example.com:/path/to/repo.git

7. Conclusion

In this article, we've covered almost everything you need to know about the git remote command, which is the starting point for working with Git. The git remote command is ultimately responsible for managing all the remote repos associated with your local Git environment, It is used in conjunction with most commands related to remote repos, such as git clone, git branch, git push, and git pull.

Managing remote repos is something that needs to be done carefully, especially as your project grows and requires more attention to detail. We hope this article gives you a good starting point for managing remote repos.

copyright for git remote

© 2023 All rights reserved.