Git: Add New Remote to a Repo

In the Git version control system you're able to push and pull code from any number of remote repositories. This is beneficial for when you want to pull in updates from someone else's fork of a project, for example. Or you may just want to have a way to link your local Git repo with the remote one on GitHub. Either way, it's beneficial to associate a remote repository to your local one. In this short article I'll explain exactly how to do that.

The command you'll want to use is git remote add, and is generally used in the following way:

$ git remote add <remote-name> <remote-location>

The remote name is helpful for being able to reference this repository without having to type out the entire location. You can also set these remotes as your default push or pull locations, shortening your Git commands even more.

For example, to add a remote origin to your repository, you would use the command like this:

$ git remote add origin [email protected]:scottwrobinson/camo.git

Once you've added a remote to your repo you can then verify it with the -v flag:

$ git remote -v
origin  [email protected]:scottwrobinson/camo.git (fetch)
origin  [email protected]:scottwrobinson/camo.git (push)

Adding Remotes for Fetch and Pull

In the output of the last command you may have noticed that there are actually two lines listed for the "origin" remote repository. This means you can actually set two different remote repositories for "origin", one for the push operation and one for fetch. This can be done with the following commands:

$ git remote set-url <remote-name> <repo-url-for-fetching>
$ git remote set-url --push <remote-name> <repo-url-for-pushing>

This kind of setup can be helpful if you're pulling in changes from the main branch of a project and then pushing any changes you make to a separate branch of your own, for example.

Author image