This post was written in a Mac environment.
Sometimes you need to manage several remote repositories from one project folder.
For example, you may need to deliver the same project to different clients.
Let's look at how to manage multiple remote repositories from a single project directory.
1. Adding the first remote repository 🔗
First, create an empty project folder.
Copy the remote URL of a repository you have already created on GitHub.
240606-154630
Add the first remote repository using the URL you copied.
git remote add origin https://github.com/user/first-repo.git
2. Adding the second remote repository 🔗
Create another repository on GitHub and use its remote URL.
Add the second remote repository.
I recommend using a name other than origin for this one.
If you use origin, operations will apply to both remote repositories simultaneously.
I named the second remote second-remote.
git remote add second-remote https://github.com/user/second-repo.git
3. Checking the configuration 🔗
Navigate to the project folder in your command-line terminal and enter the following command.
For example, the output will look like this.
origin https://github.com/user/first-repo.git (fetch)
origin https://github.com/user/first-repo.git (push)
second-remote https://github.com/user/second-repo.git (fetch)
second-remote https://github.com/user/second-repo.git (push)
4. Switching branches, pushing, and pulling 🔗
Since two remotes are connected, specify the remote's name when switching branches.
git checkout second-remote/main
After switching branches, continue your work.
git add .
git commit -m "commit message"
git push second-remote main
You can, of course, push to branches on both remotes.
git push origin main
git push second-remote main
Similarly, specify the correct remote name when pulling.
git pull origin main
git pull second-remote main
5. Removing a remote repository 🔗
To remove a remote repository, enter the following command.
git remote remove origin
git remote remove second-remote
We have looked at how to manage several remote repositories in one project directory.
This approach makes working with multiple remotes much easier.