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.
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.bStep 2: Synchronize and Rebase
Ensure no local changes, then fetch and rebase from the primary remote:
git fetch origin1
git rebase -i origin1/devIf conflicts appear, resolve them, then force‑push the updated dev branch to both remotes:
git push -f origin1 dev
git push -f origin2 devAlternatively, 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]:/commonuseppaAfter 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
