Git
Version Control
Multiple Remotes
Code Repository
Pushing Code

Git - Pushing code to two remotes

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Git can push the same branch to more than one remote destination. This is useful for backup repositories, migrations between hosting providers, mirrored internal and external repos, or workflows where the same code must land in two places.

The simplest approach: two named remotes

The clearest setup is to keep two remotes and push to both explicitly.

bash
1git remote add origin [email protected]:team/project.git
2git remote add backup [email protected]:team/project.git
3
4git push origin main
5git push backup main

This is easy to understand and easy to debug. You always know which push went where.

The tradeoff is that you need two commands. For many teams that is acceptable because it keeps intent visible and reduces surprise.

One push command with multiple push URLs

If you really want one push command, Git can attach multiple push URLs to the same remote.

bash
1git remote add origin [email protected]:team/project.git
2git remote set-url --add --push origin [email protected]:team/project.git
3git remote set-url --add --push origin [email protected]:team/project.git
4
5git push origin main

In this setup, origin may still fetch from one canonical URL, but a push to origin goes to both configured push destinations.

That pattern is convenient for mirrors, but it also means a single push failure can leave the remotes temporarily out of sync. You need to notice and repair that condition rather than assuming one command guarantees atomic success everywhere.

Choosing the right model

Use separate remotes when you want clarity, different fetch behavior, or different branch policies between destinations. Use multiple push URLs on one remote when the destinations are truly mirrors and you want one command to fan out.

The distinction matters because mirroring is not the same as collaboration. If one remote is your main development host and the other is just a backup, the multiple-push-URL setup makes sense. If the two remotes serve different audiences or workflows, separate names are usually better.

Credentials and branch rules still apply

Each destination still enforces its own authentication, protected branch rules, and server-side hooks. A push that succeeds on GitHub can fail on GitLab, or vice versa, if policies differ.

That is why mirrored pushes are operationally convenient but not magical. You still need compatible permissions and repository settings across both targets.

It is also worth deciding who owns the truth when the two remotes diverge. A mirror setup is safer when one remote is clearly primary and the second is clearly secondary.

It is also smart to verify the remote configuration before relying on it:

bash
git remote -v
git remote get-url --push --all origin

Those commands show where fetches and pushes really go, which helps avoid dangerous assumptions.

Common Pitfalls

  • Assuming one push to multiple destinations is atomic when one remote can fail independently of the other.
  • Using mirrored push URLs when the remotes actually have different policies or purposes.
  • Forgetting that fetch and push URLs can differ for the same remote.
  • Hiding important deployment behavior behind one remote name without documenting it for the team.
  • Ignoring failed mirror pushes and leaving remotes silently out of sync.

Summary

  • Git can push to two remotes either with separate remote names or with multiple push URLs on one remote.
  • Separate remotes are clearer; multiple push URLs are more convenient for true mirrors.
  • Each destination still enforces its own authentication and branch policies.
  • Use git remote -v and git remote get-url --push --all to verify the setup.
  • Mirrored pushes save effort, but you still need to monitor for partial failures.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.