If you’re a developer or sysadmin, you know that it can be difficult to keep your Git repositories organized and up-to-date. This is especially true if you have multiple developers working on different projects. To make life easier, there are a few ways to add and remove Git remotes from your work environment. Here are three tips:

  1. Add a new Git remote: Open the Terminal window and type git remote add origin mygitlab-repo mygitlab-url
    This will add the origin Git remote to your project. You can then use the following commands to access it: git remote show origin git remote get mygitlab-url
  2. Remove a Git remote: Delete the origin Git remote by typing git rm -rf origin
    Then, delete the mygitlab-repo Git remote by typing git push origin mygitlab-url

Git is a decentralized version control system, which means your local repo is the same thing as the remote repository on a site like Github. When you need to push or pull changes, you must do so using a Git remote.

What Are Git Remotes?

Because Git is a decentralized service, where local and remote repos use the same system, your local repository has no idea what the current state of your Github repo is, and vice versa. To communicate, Git clients must set up remotes to push and pull data from.

A remote is basically a URL with a name, but it’s a bit more complicated than that. Because you choose when to run git pull and git push, you can actually have multiple Git remotes. This can be useful to manage a development repository and release repository on a different platform, such as cloud-specific solutions like AWS CodeCommit.

New changes from other people in your repository must be fetched from the remote. This includes changes to your working branch, but also can fetch changes on other branches still in progress. When you need to commit something, such as adding to the HEAD or making a new branch, you must also push to the remote.

Managing Git Remotes

When you first clone or download a Git repository from the internet, it likely is configured with a remote called “origin.” You can verify this by listing the remotes, with the -v flag for verbose, which displays the URL as well:

If you want to switch remotes, like in the case of forking a Github repo and pushing updates to your own repo, you’ll need to delete the old remote:

Then, you can add a new remote. If you’re setting up a new Git repo after running git init, you will need to do this as well, since you won’t have a remote by default. The exact URL will depend on the service you’re using, but for Github, it’s available under “Code” on the main repo page. You can choose to connect over HTTPS or SSH.

Once you have the URL, you can add it with a name, usually “origin” if this is the primary remote:

Once set up, you can push and pull from it by specifying the remote name, and remote branch:

Pushing a Branch to a Different Remote

You can configure a default remote, which is usually set up to be origin. This is why, when pushing for the first time, you must set an upstream:

However, you can also configure the upstream for individual branches.

Here, Git will set the upstream used for the releasebranch to be “release,” which can be configured to a separate repository.