How To Add a Git Remote
2 min read
Usually, when working with Git, you’ll use only one remote named origin and different branches for different features and environments. Origin is the name of the remote that automatically created when you clone a repository and points to the cloned repository.
However, when collaborating on a project with a group of people, you may find using multiple Git remotes very handy.
Git remotes are pointers to the versions of the repository that are typically stored on other servers.
This guide explains how to add a new Git remote.
Adding a Git Remote
The remote repository must exist before you add the git remote to your local repository. You can create the repository on a Git hosting service such as GitHub, GitLab, and BitBucket or on your private Git server .
To add a new remote, navigate to the directory your repository is stored at and use the
git remote add command followed by the remote name, and the remote URL:
git remote add <remote-name> <remote-url>
For example, to add a new remote named
staging pointing to the
email@example.com:user/repo_name.git URL you would type:
git remote add staging firstname.lastname@example.org:user/repo_name.git
git remote command to list the remote connections and verify that the new remote was successfully added:
git remote -v
The output will look something like this:
origin https://github.com/user/repo_name.git (fetch) origin https://github.com/user/repo_name.git (push) staging email@example.com:user/repo_name.git (fetch) staging firstname.lastname@example.org:user/repo_name.git (push)
git remote add command actually does is modify the repository
.git/config file and a new connection to the remote repository.
... [remote "staging"] url = email@example.com:user/repo_name.git fetch = +refs/heads/*:refs/remotes/staging/*
You can add a new remote by editing the
.git/config file with a text editor , but using the command is much easier.
That’s it. You have successfully added a new Git remote.
To push your code to the new remote, you would use:
git push <remote-name> <branch-name>
To fetch and pull from the remote use:
git fetch <remote-name>
git pull <remote-name>
Adding a new Git remote is just a matter of one command. Git remotes are very useful and allow you to have multiple repositories.
If you hit a problem or have feedback, leave a comment below.