git push to multiple sites

The October 2018 day-long GitHub outage led many to consider having a live backup of their Git repos. Multi-remote git push is intrinsic to Git itself. Thus, automatic multi-site pushes are easy to configure for an arbitrarily large number of backup sites (GitHub, GitLab, Bitbucket, Dropbox, …).

This article covers the typical case where the main Git repository is on GitHub, but a public backup is desired on GitLab or similar site. The article assumes an existing working GitHub repo cloned to your computer username/myprog. We also assume SSH keys on both GitHub and GitLab. Use different SSH key pairs for each site for best security.

Back up the GitHub repo username/myprog to GitLab with every git push automatically as in the following section.

GitHub with GitLab backup

  1. check that the GitHub repo is setup normally on your computer:

    cd myprog
    
    git remote -v

    should be like:

    origin	https://github.invalid/username/myprog (fetch)
    origin	ssh://github.invalid/username/myprog (push)
  2. Create a new repo username/myprog on GitLab or other backup site

  3. Add the GitLab (or other) backup site:

    git remote set-url origin --push --add ssh://github.invalid/username/myprog
    git remote set-url origin --push --add ssh://gitlab.invalid/username/myprog
  4. Verify that BOTH push sites are there (in this example, github.com and gitlab.com):

    git remote -v

    should be like:

    origin	https://github.invalid/username/myprog (fetch)
    origin	ssh://gitlab.invalid/username/myprog (push)
    origin	ssh://github.invalid/username/myprog (push)

When pushing, you will see multiple Everything up-to-date – one for each site you’re pushing to.