Add a second remote origin with Git
Tags: javascript, git, webdev, beginners
Published: 2023-03-05 18:43:07


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)
```

3. 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
```

4. 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)

```

5. Make any changes to your local Git repository and commit them using the `git commit` command.

6. 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:

1. Use the git fetch command to fetch changes from both remote origins:

```
$ git fetch origin
$ git fetch second
```

2. 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:

1. Fetch changes from the first origin:
```
$ git fetch origin
```
2. 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.

3. 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