Operations 4 min read

How to Push Code to Multiple Git Repositories with a Single Command

Learn how to configure Git to push your code simultaneously to multiple remote repositories by adding multiple URLs in the .git/config file, using remote add commands, and executing a single git push command, with step-by-step examples and cautions about force pushes.

Programmer DD
Programmer DD
Programmer DD
How to Push Code to Multiple Git Repositories with a Single Command

Push Code to Multiple Git Repositories

This guide explains how to synchronize code across several remote Git repositories, such as a primary repository (A) on GitHub and a secondary repository (B) on another service, while working on a local machine (C).

Step 1: Add Remote Repositories

Use git remote add to register each remote:

git remote add origin1 git.a
git remote add origin2 git.b

Step 2: Synchronize and Rebase

Ensure no local changes, then fetch and rebase from the primary remote:

git fetch origin1
git rebase -i origin1/dev

If conflicts appear, resolve them, then force‑push the updated dev branch to both remotes:

git push -f origin1 dev
git push -f origin2 dev

Alternatively, you can configure a single remote that points to multiple URLs. Edit .git/config and add an [remote "all"] section with several url entries:

[remote "all"]
    url = git@github:luoshupeng/commonuseppa.git
    url = [email protected]:chinesedragon/commonuseppa.git
    url = [email protected]:/commonuseppa

After this setup, a single command pushes to all defined remotes: git push all For a quick force push to a single remote, you can run: git push -f origin master Warning: Using -f (force) overwrites the remote history; use it only when you are certain it will not disrupt collaborators.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ConfigurationGitVersion Controlpushmultiple remotes
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.