Pushing to Multiple Git Repositories

devops comments edit

Some time ago I developed a script to backup my GitHub account and blogged about it here. The idea is to have a backup copy always and have redundancy.

But what is instead of a local copy, we have two online copies? I’ve learned that it’s very easy to achieve as Git supports pushing to multiple repositories at the same time.

For this I created a repository in CodeCommit which is free up to 50GB.

The secret to achieve this is the following command:

git remote set-url --add --push

Steps to push to multiple repositories

01. First run it with the original value (which can be found by running git remote -v command)

git remote -v

02. Copy the URL for push and run the following command:

git remote set-url --add --push origin {push URL from above}

03. Then run it with the 2nd remote like this:

git remote set-url --add --push origin ssh://git-codecommit.eu-west-2.amazonaws.com/v1/repos/{repo_name}

Testing the new repo

When you run git remote -v again you should see something like this:

origin	git@github.com:volkanx/{repo_name}.git (fetch)
origin	git@github.com:volkanx/{repo_name}.git (push)
origin	ssh://git-codecommit.eu-west-2.amazonaws.com/v1/repos/{repo_name} (push)

Now put a test file in the local repo and push it with

git push -u origin master

It should appear in both remote repositories. It also works well with GUI tools (I use SourceTree).

This way I get to have an online backup without too much hassle and I get to change my provider whenever I want if I wanted to.