DEV Community

Hash
Hash

Posted on • Updated on

Add a second remote origin with Git

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:

  1. Open your terminal or command prompt and navigate to the local Git repository that you want to push to two repositories.
  2. 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)
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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)

Enter fullscreen mode Exit fullscreen mode
  1. Make any changes to your local Git repository and commit them using the git commit command.

  2. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

  1. Use the git fetch command to fetch changes from both remote origins:
$ git fetch origin
$ git fetch second
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

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:

  1. Fetch changes from the first origin:
$ git fetch origin
Enter fullscreen mode Exit fullscreen mode
  1. Merge the changes from the first origin into your local repository:
$ git merge origin/master
Enter fullscreen mode Exit fullscreen mode

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.

  1. Push the changes to the second origin:
$ git push second master
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

Collapse
 
adya_kalhari profile image
Adya Kalhari

Great article! Adding a second remote origin can be really useful for projects that require deployment to multiple repositories. If anyone's interested in learning more about Git and version control, I recently wrote a blog post that might help: Mastering Git. Check it out and let me know what you think!