How to sync my commits with multiple repositories in git?

There are many ways of doing this; some include custom scripts, apps, and even some paid services. We will do this using the most straightforward way of having multiple URLs for a single remote.

Let's say you have added your original remote like this:

git remote add origin origin-host:path/to/project.git

What many people don't know is that you have the option to add multiple hosts for the same remote. You can do this by simply adding your second host to the same remote like this:

git remote set-url --add origin second-host:path/to/project.git

You can do this whenever you want to have any hosts.

git remote set-url --add origin third-host:path/to/project.git
git remote set-url --add origin fourth-host:path/to/project.git
git remote set-url --add origin fiveth-host:path/to/project.git

Your config file will look something like this:

[remote "origin"]
    url = origin-host:path/to/project.git
    url = second-host:path/to/project.git
    url = third-host:path/to/project.git

Now you can push your changes with a single command, which will be synced to all repositories.

git push origin {branch name}

Further Reading:

How to push to multiple git remotes at once. Useful if you keep mirrors of your repo.
How to push to multiple git remotes at once. Useful if you keep mirrors of your repo. - git-pushing-multiple.rst

Originally Posted On: StackOverflow

Subscribe to GIT.WTF!?!

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe