DEV Community

Cover image for Add multiple remotes to a Git repository.
ishrat
ishrat

Posted on • Edited on

Add multiple remotes to a Git repository.

As a programmer, one of the best things that has happened to me is Git!
Git allows you to keep your code in sync with a remote repository that is shared with your team members.

Adding multiple remotes

When you run the command 'git init', you are creating a Git repository on your local machine. The purpose of this repository is to keep your code changes organized and ready to be synchronized with a remote Git repository. To sync your code changes with a remote repository, you need to specify the location of the remote repository.

The first step is to add remote repos to your project.

# Syntax to add a git remote
git remote add REMOTE-ID REMOTE-URL
Enter fullscreen mode Exit fullscreen mode

By convention, the original/primary remote repo is called the origin. Here’s a real example:

# Add remote 1: GitHub.
git remote add origin git@github.com:example/example1.git
# Add remote 2: BitBucket.
git remote add upstream git@bitbucket.org:example/example2.git
Enter fullscreen mode Exit fullscreen mode

In the above example, we add the remote repository of a project. Use the above command to add one or more remote Git repos(using your project URL) – make sure that each repo has its Remote ID, i.e. origin, upstream in the above example.

List all remotes

If you want to view a comprehensive list of all the remotes, you can do so by using the following command:

$git remote -v
origin      git@github.com:jigarius/toggl2redmine.git (fetch)
origin      git@github.com:jigarius/toggl2redmine.git (push)
upstream    git@bitbucket.org:jigarius/toggl2redmine.git (fetch)
upstream    git@bitbucket.org:jigarius/toggl2redmine.git (push)
Enter fullscreen mode Exit fullscreen mode

Remove a remote

If you no longer need a remote you added, you can remove it by following these steps:

# The syntax is: git remote remove REMOTE-ID
git remote remove upstream
Enter fullscreen mode Exit fullscreen mode

Note: In my case, it's Upstream, but it will be your remote-id; it could be anything you specified such as origin, origin2, etc.

Conclusion

Synchronizing code across multiple Git repositories and pushing it to multiple remotes is a straightforward process that can be quite useful when managing mirrors or copies of the same repository.

Top comments (0)