Signup/Sign In

Git Remote

A remote repository(also called remote) is a shared central repository where developers can push and pull changes. Git provides us with the Git Remote command which can be used to work with remote repositories. We have the Git Push and the Git Pull commands that can be used to push or pull changes but first, we need to create a connection using the Git Remote command. Let's learn how to use this command.

Git Remote

Git Remote Command

The Git Remote command is used to establish connections with remote repositories. This makes it very easy to push and pull changes and work with commands like Git Push, Git Pull, and Git Fetch. It also allows us to have better and more intuitive names for the remote repositories(like origin), instead of using their direct URLs. Let's learn how to use this command to create and modify remote connections.

Creating New Remotes

We can create a new remote connection by using the add option with the Git Remote command. We need to specify the name that we want to give to the remote and also the URL where the repository resides.

$ git remote add <remote-name> <remote-repo-URL>

Viewing Existing Remotes

We can view the remote connections of our local repository with remote repositories by using the Git Remote command. Simply run the GIt Remote command without any flags or options to view these remotes.

$ git remote 

We also have the -v flag or the --verbose option which will also tell us the URLs of the remote repositories in addition to the remote names. It also tells us the URLs where changes will be pushed and from where the changes will be fetched.

$ git remote -v

To view even more information about remotes like the branches present, the HEAD position, etc., we can use the show option with the command.

$ git remote show <remote-name>

Removing Remotes

To remove a remote, use the rm option with the Git Remote command and pass the name of the remote that you wish to delete.

$ git remote rm <remote-name>

Renaming Remotes

We can also rename an existing remote by using the rename option with the Git Remote command. Pass the old remote name and the new name with the command.

$ git remote rename <old-remote-name> <new-remote-name>

Changing Remote URL

Sometimes we may migrate our remote repository to a new URL. In such cases, we must also update our remote connections on the local repository to make sure that we are pushing and pulling changes from the correct repository. Use the set-url option to change the URL of an existing remote.

$ git remote set-url <remote-name> <new-remote-URL>

These are the most important flags and options that we will often use with the Git Remote command. Let's see a practical demo to understand these commands better.

First, we will create a new remote connection by using the add option. We will name this remote demo.

Adding a new remote

Next, let's view this remote by using the Git Remote -v command. We can see the remote name and the repository URL by using this option.

Viewing the remote name and the remote URL

We can also view additional information by using the show option.

Using the show option with Git Remote to view additional information

Let's now rename this remote. After renaming, we can verify whether it was renamed by simply running the Git Remote command.

Renaming a remote

Viewing the remote to check if it was properly renamed

We can easily push and pull from the remote repository by using this remote name. For now, let's just remove this remote.

Removing the remote from our local repository

Summary

We will be working with remote repositories quite often and we must know how to connect our local repository to the remote repository. We use the Git Remote command to establish remote connections. This helps us to push and pull changes and also allows us to give convenient names to remote URLs. Git Remote has a lot of other options to modify and manipulate remotes. We learned how to create, view, rename and remove remotes in our local repository.