Add a second remote origin with Git
Hash
Posted on March 5, 2023
Sometimes you need to have a second remote origin for your project to be able to do something special, for me it was adding an auto deployment for the website, while I didn't access to origin master as repository owner.
In this case I'm going to have a copy from the project and while it should be sync with the origin master whenever it changes, today we're going to see how we can achieve it.
To add a second remote origin to Git and push changes to two repositories on GitHub, you can follow these steps:
- Open your terminal or command prompt and navigate to the local Git repository that you want to push to two repositories.
- Use the
git remote -v
command to list the existing remote origins for the repository. You should see something like this:
$ git remote -v
origin https://github.com/username/repo.git (fetch)
origin https://github.com/username/repo.git (push)
- Use the
git remote add
command to add a second remote origin to the repository. For example, if you want to add a remote origin named "second" with the URL https://github.com/username/second-repo.git, you can use the following command:
$ git remote add second https://github.com/username/second-repo.git
- Use the
git remote -v
command again to confirm that the new remote origin has been added:
$ git remote -v
origin https://github.com/username/repo.git (fetch)
origin https://github.com/username/repo.git (push)
second https://github.com/username/second-repo.git (fetch)
second https://github.com/username/second-repo.git (push)
Make any changes to your local Git repository and commit them using the
git commit
command.Use the
git push
command to push your changes to both remote origins. To push to both origins at once, you can use the--all
option:
$ git push --all
This will push all branches to both remote origins. Alternatively, you can specify a specific branch to push to both origins:
$ git push origin branch-name
$ git push second branch-name
This will push the specified branch to both remote origins.
Syncing two remote origins
To check if your two remote origins are in sync, you can use the git fetch
command to fetch changes from both origins and compare them. Here's how you can do it:
- Use the git fetch command to fetch changes from both remote origins:
$ git fetch origin
$ git fetch second
- Use the
git log
command to compare the commit history of the two remote origins. For example, to compare the master branch on both origins, you can use the following command:
$ git log origin/master
& git log second/master
If the second origin is behind the first origin, you can sync them by fetching changes from the first origin and then pushing them to the second origin. Here's how you can do it:
- Fetch changes from the first origin:
$ git fetch origin
- Merge the changes from the first origin into your local repository:
$ git merge origin/master
Note that "origin/master" is the name of the branch you want to merge from the first origin. Replace "master" with the name of the branch you want to merge.
- Push the changes to the second origin:
$ git push second master
Note that "second" is the name of the second remote origin, and "master" is the name of the branch you want to push. Replace "master" with the name of the branch you want to push.
That's it,
hope it would be helpful.
let me know any questions in the comments
HASH
Posted on March 5, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
May 17, 2024