Push to Two Git Remotes: Setup & Sync

Push to Two Git Remotes: Setup & Sync

~ 2 min read

Why Push to Two Remotes?

Using two Git remotes is common when:

  • You want a mirror on a self‑hosted server and GitHub/GitLab.
  • You’re transitioning between hosting services and want to keep both in sync.

What follows is a walk through of how to set it up and keeping both remotes updated, with two different but similar approaches.

Adding and pushing to two remotes manually

1. Add Your Remotes

Start with an existing repo, or clone your primary remote:

git clone git@github.com:you/your-repo.git
cd your-repo

Check your remotes:

git remote -v
# origin   git@github.com:you/your-repo.git (fetch)
# origin   git@github.com:you/your-repo.git (push)

Add a second remote (e.g. backup) pointing to the mirror repo:

git remote add backup git@backup.com:you/your-repo-backup.git

Confirm it’s there:

git remote -v
# origin   git@github.com:you/your-repo.git (fetch)
# origin   git@github.com:you/your-repo.git (push)
# backup   git@backup.com:you/your-repo-backup.git (fetch)
# backup   git@backup.com:you/your-repo-backup.git (push)

2. Push to Both Remotes Manually

You can push to them separately:

git push origin main
git push backup main

If you use multiple branches (develop, feature/*, etc.), you’ll repeat the push commands per branch.

3. Automate with a Git Alias

To avoid typing two commands, add an alias in your ~/.gitconfig:

[alias]
  pushall = "!git push origin --all && git push backup --all"
  push-tags = "!git push origin --tags && git push backup --tags"

Now you can run:

git pushall
git pushtags

Nice and easy!

OR Push to Multiple Remotes at Once

Git lets you set multiple URLs for a single remote. For example, you can combine both remotes under origin:

git remote set-url --add origin git@github.com:you/your-repo.git
git remote set-url --add origin git@backup.com:you/your-repo-backup.git

Check:

git remote -v
# origin git@github.com:you/your-repo.git (fetch)
# origin git@github.com:you/your-repo-backup.git (fetch)
# origin git@github.com:you/your-repo.git (push)
# origin git@backup.com:you/your-repo-backup.git (push)

Now a simple:

git push origin main
git push origin --tags

Pushes to all URLs configured under origin. Super‑handy.

Note: Remotes share history/tracking. If one fails, Git stops and reports an error. You’ll see which push failed and can retry.

Cloning from Multiple Push URLs

When you clone:

git clone git@github.com:you/your-repo.git
cd your-repo
git remote set-url --add origin git@backup.com:you/your-repo-backup.git

It behaves the same: one “origin” pulling from both, pushing to both.

Verifying Everything Works

Create a test commit:

echo "# test" >> TEST.md
git add TEST.md
git commit -m "Add TEST.md"

Push:

git push origin main

Verify the file exists in both repos (via web UI or git ls-remote).

Test tags:

git tag v1.0-test
git push origin --tags

all posts →