
How to Push to Multiple Git Repositories Simultaneously
Keeping two distinct Git repositories in sync—like a public GitHub repository and a private internal Git server—can be a tedious chore if you are manually pushing to them one by one. If one push succeeds and the other is forgotten, your codebases immediately drift out of sync.
The most elegant solution is configuring a “dual-push” remote. This allows you to type a single git push command and have Git automatically deploy your code to both servers sequentially.
Here is how to configure a pristine dual-push environment from scratch, align your branch names, and troubleshoot common setup errors.
The Setup: Configuring Your Remotes
For this guide, we will assume you want your primary working branch to be main, but your secondary internal server defaults to an older naming convention (like master or mainline).
1. Make your initial commit:
This step prevents the ‘src refspec does not match any’ error. Before Git can track or rename a branch, it needs at least one saved change. If this is a fresh repository, stage and commit your files:
git add . git commit -m "Initial commit"
2. Standardize the local branch name:
This step aligns your local environment. Force the local branch rename to main so it perfectly matches modern Git standards:
git branch -M main
3. Establish the base remote:
Add your primary connection (e.g., GitHub) as the base origin:
git remote add origin https://github.com/YourOrg/YourRepo.git
4. Set the first push URL:
This step overrides default behavior. Explicitly tell Git to use GitHub for pushing. Once you set one push URL, Git stops using the default fetch URL for pushes:
git remote set-url --push origin https://github.com/YourOrg/YourRepo.git
5. Set the second push URL:
Add your secondary repository (e.g., an internal Git server) to that exact same origin:
git remote set-url --add --push origin ssh://git.internal-server.com/pkg/YourRepo
(Tip: Run git remote -v. You should see one fetch URL and two push URLs.)
6. Push to both servers:
Push your main branch up to both servers simultaneously. This will update main on GitHub, and create a brand new branch called main on your internal server:
git push -u origin main
The Cleanup: Retiring the Old Default Branch
Because your internal server likely defaulted to an older branch name (like master), you now have two branches on that server: the old default, and your newly pushed main branch.
To keep things clean, you need to transition the server to use main and retire the old branch.
- Change the Default Branch: Log into the web interface for your internal Git server. Navigate to the repository settings, find the “Default branch” or “HEAD” setting, and switch it to
main. - Retire the Old Branch: Depending on your server’s permissions, either delete the old branch entirely or select the option to Deprecate it (which locks it and hides it from view without destroying audit histories).
- Prune Locally: Back in your terminal, tell your local Git to clean up any references to branches that no longer exist on the servers:
git fetch --prune