Table of contents
- 1. Clone the First Repository
- 2. Create a Second Main Branch
- 3. Add a Second Remote
- 4. Push the Branches to Different Repositories
- 5. Working with Different Repositories
- Conclusion
In this article, I will guide you through setting up your codebase to push to the main branch of two different repositories. This isnโt something most people usually need to do, but here I am, working on a codebase where I have a base code repository. Other repos branch out from this base, each with its own changes, and I need to sync them up from time to time.
1. Clone the First Repository
First, clone the primary repository that will be linked to your first main branch (usually called main
):
01: git clone https://github.com/yourusername/my-repo.git
02: cd my-repo
2. Create a Second Main Branch
Now, create a new branch for the second repository:
$ git checkout -b client-main
This gives you two branches: main
for your base repo and client-main
for the clientโs repo.
3. Add a Second Remote
Next, add the clientโs remote repository so you can push changes there.
$ git remote add client-repo https://github.com/clientsusername/client-repo.git
At this point, you have two remotes: origin
(which points to your base repo) and client-repo
(which points to the clientโs repo).
4. Push the Branches to Different Repositories
First, push the main
branch to your base repository:
01: git checkout main
02: git push --set-upstream origin main
Then, switch to the client-main
branch and push it to the clientโs repository:
01: git checkout client-main
02: git push --set-upstream client-repo client-main
5. Working with Different Repositories
Now, when I make changes on the main
branch, I can push them to my own repository (origin
). When I work on the client-main
branch, I can push those changes to the clientโs repository (client-repo
):
01: # My repository
02: git checkout main
03: git add .
04: git commit -m "Base repo updates"
05: git push origin main
06:
07: # Client's repository
08: git checkout client-main
09: git add .
10: git commit -m "Client-specific updates"
11: git push client-repo client-main
Conclusion
By setting up two main branches with different remotes, I can manage changes between similar codebases with different needs. This setup allows me to work from a single codebase but sync changes between multiple repositories when necessary.
Here is another article you might like ๐ How To Handle File Uploads In Node.js Using the express-fileupload package