๐ Git Multi-Remote Workflow: How to Manage and Push Code to Multiple Repositories (origin & old-origin) Like a Pro
Introduction
In real-world developmentโespecially when you're working as a CTO or managing multiple environmentsโyou often face a situation like:
- One repo for production
- One repo for backup / legacy
- One repo for client delivery
- One repo for internal development
And suddenly, your simple Git workflow becomes complex.
You ask:
๐ โHow do I manage different codebases across multiple repos without breaking anything?โ
This guide solves that completely.
By the end, you will:
- Understand how Git multi-remote works
- Switch between repos safely
- Handle different code in
originandold-origin - Use production-level strategies (branch isolation, syncing, migration)
- Avoid dangerous mistakes like overwriting code
Table of Contents
- What is Multi-Remote in Git
- Understanding origin vs old-origin
- Core Concept: Code vs Remote (Most Important)
- Real Workflow Scenarios
- Step-by-Step Implementation (Production Setup)
- Managing Different Codebases Safely
- Architecture-Level Strategy (CTO Thinking)
- Comparison: Single Repo vs Multi Repo
- Common Mistakes & Fixes
- Expert Pro Tips
- FAQ
- Conclusion
- Meta Description
1. What is Multi-Remote in Git
Git allows you to connect your local project to multiple remote repositories.
Example:
origin โ New Repo
old-origin โ Old Repo
This is called multi-remote setup.
2. Understanding origin vs old-origin
In your case:
origin = New repository
old-origin = Old repository
These are just aliases, not actual code.
๐ Important:
Remote names do NOT store code. They only define destinations.
3. Core Concept: Code vs Remote (MOST IMPORTANT)
This is where most developers get confused.
Reality:
| Component | Meaning |
|---|---|
| Local branch | Your actual code |
| Remote (origin) | Where you push |
| Remote (old-origin) | Another destination |
๐ Key Rule:
Git always pushes your current local branch code, not remote code.
4. Real Workflow Scenarios
Scenario 1: Push same code to both repos
git push origin main
git push old-origin main
Scenario 2: Different code in both repos
This is your real case.
You must:
- Pull code from respective repo
- Switch context locally
- Then push
5. Step-by-Step Implementation (Production Setup)
Step 1: Add both remotes
git remote rename origin old-origin
git remote add origin git@github.com:yourusername/new-repo.git
Step 2: Fetch all data
git fetch origin
git fetch old-origin
Step 3: Create separate local branches
git checkout -b new-main origin/main
git checkout -b old-main old-origin/main
Now your structure:
| Branch | Code Source |
|---|---|
| new-main | New repo |
| old-main | Old repo |
Step 4: Work with branches
Use new repo code:
git checkout new-main
Use old repo code:
git checkout old-main
Step 5: Push correctly
Push new repo code:
git checkout new-main
git push origin new-main:main
Push old repo code:
git checkout old-main
git push old-origin old-main:main
6. Managing Different Codebases Safely
Golden Rule:
One branch = One source of truth
Never mix:
origin/mainold-origin/main
Instead:
new-mainold-main
If you want to copy code between repos
Copy old repo โ new repo
git checkout old-main
git push origin old-main:main
Copy new repo โ old repo
git checkout new-main
git push old-origin new-main:main
7. Architecture-Level Strategy (CTO Thinking)
In enterprise systems, this is how multi-repo is used:
Use Cases
| Use Case | Strategy |
|---|---|
| Backup system | old-origin |
| Migration | move code gradually |
| Multi-client deployment | separate repos |
| Version isolation | repo-based control |
Architecture Model
LOCAL SYSTEM
/ \
new-main branch old-main branch
| |
origin old-origin
(new repo) (old repo)
8. Comparison Table
| Approach | Pros | Cons | Best Use Case |
|---|---|---|---|
| Single Repo | Simple | No flexibility | Small projects |
| Multi Remote | Flexible | Needs discipline | Enterprise |
| Multi Branch | Controlled | Slight complexity | Dev teams |
| Multi Repo | Full isolation | Hard to sync | Large org |
9. Common Mistakes (Real-World)
โ Mistake 1: Pushing wrong code
git push origin main
Without checking branch
โ
Fix:
git branch
โ Mistake 2: Mixing repos
Using one branch for both repos
โ
Fix:
Use separate branches
โ Mistake 3: Force pushing blindly
git push --force
โ
Fix:
Use only when required
โ Mistake 4: Not fetching remotes
Leads to outdated code
โ
Fix:
git fetch origin
git fetch old-origin
10. Expert Pro Tips
๐ฅ Pro Tip 1: Always verify before push
git status
git branch
๐ฅ Pro Tip 2: Use aliases
alias gpo="git push origin main"
alias gpol="git push old-origin main"
๐ฅ Pro Tip 3: Protect main branch
Use:
- Protected branches in GitHub
- PR-based deployment
๐ฅ Pro Tip 4: Use staging branches
staging-main
production-main
๐ฅ Pro Tip 5: Use tagging for releases
git tag v1.0
git push origin v1.0
11. FAQ
1. Can I push different code to two repos?
Yes. Use separate local branches.
2. Can I sync both repos automatically?
Yes, but better to control manually for safety.
3. What happens if both repos have different history?
You must:
- merge OR
- force push carefully
4. Is multi-remote safe?
Yes, if you follow branch discipline.
5. Should I use this in production?
Yes, especially for:
- migration
- backup
- multi-client systems
12. Conclusion
Managing multiple Git repositories is not complexโif you follow the right architecture.
Key Takeaways:
- Remote = destination
- Branch = code
- Always separate codebases using branches
- Never mix repositories blindly
Your Action Plan:
- Setup
originandold-origin - Create
new-mainandold-main - Always switch branch before push
- Push carefully based on destination
Top comments (0)