git remotes

Pushing to Multiple Remotes with git and SSH

  1. Ensure necessary SSH keys have been created - if not, use “ssh-keygen” and fill out the fields. “id_rsa” is the private pair, while “id_rsa.pub” is the public pair.
  2. If using multiple ssh keys, create a ~/.ssh/config file. For example, the contents of my file looks like:
        Host *sr.ht
            IdentityFile ~/.ssh/srht.id_rsa
            PreferredAuthentications publickey
        Host gitlab.com
            IdentityFile ~/.ssh/gitlab.id_rsa
            PreferredAuthentications publickey
        Host bitbucket.org
            IdentityFile ~/.ssh/bitbucket.id_rsa
            PreferredAuthentications publickey
        Host github.com
            IdentityFile ~/.ssh/github.id_rsa
            PreferredAuthentications publickey

and the naming convention for my keys:

        private: HOST.id_rsa
        public: HOST.id_rsa.pub
  1. Add public key to respective git host service
  2. Navigate to git repository and configure the primary remote:
        git checkout BRANCH
        git branch -u origin/BRANCH

Note: The original/primary remote is typically named origin.

  1. Add the remote URL:
        git remote set-url REMOTE_NAME git@foobar.com:harthan/repo_name.git
  1. Push to multiple remotes with a single “git push” command:
        # Create a new remote called "all" with the URL of the PRIMARY repo
        git remote add all git@foobar.com:foobar/repo.git

        # Add the origin remote as a push URL
        git remote set-url --add --push all git@foobar.com:foobar/repo.git

        # Add a different URL to a remote to push to
        git remote set-url --add --push all git@foo.bar.com:foobar/repo.git
  1. To modify the remote or the remote URL:
        # To list all remotes associated with a git repo
        git remote -v

        # To *remove* a remote
        git remote remove REMOTE_NAME

        # To change a remote URL
        git remote set-url REMOTE git@foobar.com:foobar/repo.git

        # To *remove* a push URL from the remote
        git remote set-url --delete --push REMOTE git@foobar.com:foobar/repo.git
  1. Push to all remote repos:
        git push all (BRANCH)