In this lesson, you'll explore Git remote repositories, and practice using the git remote command to view and manage the connections between your local and remote repositories. To this end, you'll work through the Git Remote command, Git Remove Remote, Git Add Remote, Git Remote Set URL, Git Rename Remote, and Git Remote Show.
What is a Git Remote?
A Git remote is Git's way of defining the online repository where your code is stored, for example, on GitHub. It is a URL that points to the location of that repository on the web or on a different Git server.
The remote repository is shared with your teammates, while the local repository is your personal scratch space. Software development teams are often distributed around the world. The remote repo is used for collaboration, to share modifications, exchange features, and communicate about the shared work.
You can do a set of operations with Git on a remote server. The most important ones are clone, fetch, push, and pull, and you will learn more about each of these in the upcoming lessons. First, you'll learn more about the command git remote.
Git Remote Command
The git remote command enables you to view and manage the connections between your local and remote repositories. For instance, you can use it to confirm that your local repository is correctly linked to a remote server.
To do so, run git remote from the root folder of that Git project. You can give this a try with one of your existing repositories, testing_git, that you created on your GitHub account earlier in the course. Clone the repository to your local machine into a new directory:
Info: For more on how to create a repository on GitHub, check out Creating a Repository in GitHub from earlier in the course. For more on cloning, you can also check out the exercise, Clone a GitHub Repository.
$ git clone https://github.com/<YOUR_GITHUB_USERNAME>/testing_git.git
$ cd testing_git
$ git remote
After running the command, you will see the word "origin" show up. This is the default name that Git uses for a remote server. Git assigns this name when you clone a remote repository to your local machine.
Now, that doesn't tell you a whole lot so far! You can make the output more useful by adding the flag -v to the command. This stands for verbose and means that you want Git to tell you more:
$ git remote -v
Now Git will speak to you a little less cryptically and present you with some valuable information about your remote repository, namely its name and its URL:
origin https://github.com/CodingNomads/testing_git.git (fetch)
origin https://github.com/CodingNomads/testing_git.git (push)
Info: Your URL will show your own GitHub username instead of CodingNomads.
You'll notice that you see the word origin twice in that output, followed by two identical URLs. The only difference between the two lines is the word at the end:
- (fetch): The remote location Git will use to fetch information from, often via
git pull. - (push): The remote location Git will use to push local changes to. The command is
git push.
The output, therefore, tells you that when you push or pull from origin, Git will perform these operations on the repo located at the URL following the word origin. Similar to a variable in programming, you use the word origin as a reference to the URL so that you don't have to type out the whole URL each time. Thanks for making it easier, Git!
You might wonder about the redundancy of displaying the same URL twice. The reason for this is that you can fetch from a different remote repository than where you push your changes. In that case, these URLs wouldn't be identical and point to different locations. However, most of the time, you will want to fetch from the same repository you push to.
Git Remove Remote
To remove a remote connection from a repository, run the git remote command with the remove, or rm for short, option. The git remote rm command takes one argument, the name of the remote that you want to remove:
# git remote rm <Remote Name>
$ git remote rm origin
If you want to use the more verbose command that does the same, it will look like this:
# git remote remove <Remote Name>
$ git remote remove origin
After running this command, you can verify with git remote -v that the remote was removed successfully. At this time, you should see no output from running this command, since there is currently no remote associated with the Git project. Once you've deleted the reference to a remote, all remote tracking branches and configuration settings associated with that remote are also removed.
Info: The git remote rm command does not delete the remote repository from the server. It simply removes the remote and its references from your local repository.
Now that you've learned how to remove a remote, you'll next learn how to add a remote to your local Git repository.
Git Add Remote
To add a new remote, use the git remote add command. It takes two arguments:
- Remote Name This is set to "origin" by default, but it can be anything else, e.g., "whaleshark".
- Remote URL The URL of the remote repository you'd like to add. The remote repository needs to already exist so you have a valid URL to point to
As you've seen, when you run git clone <URL> and clone a repository to your local machine, Git automatically configures a remote for that repository. This default remote is named "origin" and it points to the URL from which you cloned the project.
But what if you removed that remote? In your case, since you already have the testing_git repo on GitHub and you only removed the remote from your local Git project of that same repo, you can simply re-add the remote. You can do so using the git remote add command like below:
$ git remote add whaleshark https://github.com/CodingNomads/testing_git.git
Note: Your URL will need to show your own GitHub username instead of "CodingNomads". Also, you can choose any other name for the remote. This example uses "whaleshark" to exemplify that the name is arbitrary and that you can choose it yourself.
Now, when we run git remote -v, you'll see that you have reconnected your local Git repo to your remote testing_git repository on GitHub. However, there is one difference: The name of the remote has changed from the default "origin" to the custom "whaleshark" string.
Now, this worked fine with the repository you cloned from GitHub earlier. But what if you created your Git repository locally with the git init command?
Push Local Repository to GitHub
If you create a local repository on your machine using git init and you'd like to push that project to GitHub, you use the same command: git remote add. However, you need to create the remote repository on GitHub before you can establish the connection. In a way, that makes sense. You need to have a URL that points to a resource on GitHub's servers that actually exists before you can connect to it.
To demonstrate this, create a new remote repository on your GitHub account and name it remote_repo_test. Avoid initializing the repo with a README.md file, since that would require you to sync changes before being able to push your local work there. You will learn about this later on. After the new GitHub repository is created, you'll see the following page:
To get the URL to establish the connection, you need to copy the HTTPS URL shown on that page. In this case, the URL is:
https://github.com/CodingNomads/remote_repo_test.git
Note: Your URL will again be slightly different, as it will point to your own GitHub account, not CodingNomads' account.
After you create an empty repository on GitHub and copy the URL that points to that repository, you can now add it as a remote to a new local Git repo. But wait! You haven't created a new local Git repo yet. So go ahead and do that first, and then add the remote to it. The following code snippet will take you through the necessary steps:
- Create a new folder on your desktop
- Move into that folder on your CLI
- Initialize an empty Git repo
Go ahead and get it done:
$ cd ~/Desktop
$ mkdir new_remote
$ cd new_remote
$ git init
Verify that there is currently no remote associated with this fresh and sparkling new local Git repo:
$ git remote -v
Running this command should show you no output at this point. Now it's time to add the connection to your remote repository that you created on GitHub before, following this command formula: git remote add <nickname> <url>:
$ git remote add origin https://github.com/CodingNomads/remote_repo_test.git
If you now run the git remote -v command again, you will see that your remote was successfully added to your local Git repo. This means that the connection has been established, and you are now able to push to and fetch from the remote GitHub repository and keep it in sync with your local one.
Info: Remember that "origin" is just a nickname. It can be any word. "origin" is the default nickname for Git's primary remote.
Wait a second... "primary remote"? Can there be more than one?
In real-life software development, it is very common to work with multiple remotes on one project. This means that your local Git repository is connected to multiple remote repositories at the same time.
For instance, if you wanted to clone a repository from CodingNomads' GitHub, and then push that project to your own GitHub, you could do this using multiple remotes. For now, just keep in mind that this is possible, and you'll learn how to do it in a later section.
Git Remote Set URL
Sometimes, you will want to swap out your remote's URL in one of your local Git repos with a new one. There is an easier way to do this than removing it and adding the new one afterward. You can use the following command to change the URL of an existing remote:
#git remote set-url <Remote Name> <Changed URL>
$ git remote set-url origin https://github.com/CodingNomads/remote_repo_test_2.git
Note: The command above will only work on a repository that has an existing remote configured. It does not create a remote.
Keep in mind that this command only changes the URL of an existing remote. If your project doesn't have a remote with the name "origin" already configured, then the command will fail.
Git Remote Rename
If you'd like to rename an existing remote, you can use the git remote rename command. In the example below, you'll rename the remote named "origin" to "orangutan":
# git remote rename <old name> <new name>
$ git remote rename origin orangutan
Again, this command will only work if you have a remote under the name "origin" already configured.
Git Remote Show
To see additional information about a specific remote, you can use the git remote show <Remote Name> command:
$ git remote show orangutan
After running the command, Git will provide you with some more detailed information about the specific remote you are asking about:
You can see that the output is similar to what you receive when running git remote -v, except that here you are presented only with information about the remote you specified. It contains the URLs for fetching and pushing, but also shows additional relevant information.
If you had multiple remotes on your local Git repo, this command would show only the information related to the remote you specified when running it.
Summary: Git Remote
Below is a summary of the topics covered in this lesson:
- What is a Git Remote? A Git remote is Git's way of defining the online repository where your code is stored. It is a URL that points to the location of that repository on the web or on a different Git server.
- Git Remote Command. The
git remotecommand enables you to view and manage the connections between your local repository and remote repositories. - Git Remove Remote. Use
git remote removeorgit remote rmto remove a remote connection from a repository. - Git Add Remote. To add a remote, run
git remote add <Remote name> <Remote URL> - Git Remote Set URL. To change the URL of an existing remote, use
git remote set-url <Remote Name> <Changed URL>. - Git Rename Remote. Rename an existing remote by running
git remote rename <old name> <new name>. - Git Remote Show. To see additional information about a specific remote, use the
git remote show <Remote Name>command.
This wraps up the most important information about Git remotes. For more information, please refer to the git remote docs. Next, a video about all these fun Git Remote functions!