Skip to content

Instantly share code, notes, and snippets.

Created February 9, 2016 11:41
Star You must be signed in to star a gist
Save rvl/c3f156e117e22a25f242 to your computer and use it in GitHub Desktop.
How to push to multiple git remotes at once. Useful if you keep mirrors of your repo.

Pushing to Multiple Git Repos

If a project has to have multiple git repos (e.g. Bitbucket and Github) then it's better that they remain in sync.

Usually this would involve pushing each branch to each repo in turn, but actually Git allows pushing to multiple repos in one go.

If in doubt about what git is doing when you run these commands, just edit .git/config (git-config(1)) and see what it's put there.


Suppose your git remotes are set up like this:

git remote add github
git remote add bb

The origin remote probably points to one of these URLs.

Remote Push URLs

To set up the push URLs do this:

git remote set-url --add --push origin
git remote set-url --add --push origin

It will change the remote.origin.pushurl config entry. Now pushes will send to both of these destinations, rather than the fetch URL.

Check it out by running:

git remote show origin


A branch can push and pull from separate remotes. This might be useful in rare circumstances such as maintaining a fork with customizations to the upstream repo. If your branch follows github by default:

git branch --set-upstream-to=github next_release

(That command changed branch.next_release.remote.)

Then git allows branches to have multiple branch.<name>.pushRemote entries. You must edit the .git/config file to set them.

Pull Multiple

You can't pull from multiple remotes at once, but you can fetch from all of them:

git fetch --all

Note that fetching won't update your current branch (that's why git-pull exists), so you have to merge -- fast-forward or otherwise.

For example, this will octopus merge the branches if the remotes got out of sync:

git merge github/next_release bb/next_release


Copy link

Thank you very much!

Copy link

very nice

Copy link

Ops. I just realized that I have the gitlab access token stored locally but github requires a password. So now when I push, it only pushes to gitlab. What should I do in this case?

Copy link

GretaP commented Jul 1, 2020

Thanks for this - I could have figure this out on my own but this made everything SO SIMPLE. Really great job reviewing everything

Copy link

thanks a lots

Copy link

tewshi commented Jul 13, 2020


Copy link


Copy link

That is awesome, thank you so much!

Copy link

Super useful, big thanks!

Copy link

I don't know why but for some reason I had to swap the push and add parameter: git remote set-url --push --add origin url

Copy link

Thank you!

Copy link


Copy link

Thanks this is gold.

Copy link

Thanks Really cool 👍

Copy link

AsifulNobel commented Jul 10, 2021

Thanks a lot for making this 👋

Copy link

You're the first hit returned by a google search for "git push to multiple remotes" 💯 thanks for this.

One refinement I'd like to make, maybe you can help? I have a pre-push hook in my repo. It looks like when I push, the hook is run for every remote I have configured. Is that expected behavior? If so, is there way to configure a hook to run only once, or a way write a hook such that it runs once per push command?

Copy link

I'll have to take a look at this later.

Copy link

malantin commented Aug 4, 2021

Very helpful. Thank you!

Copy link

neat 📸

Copy link

rhc822 commented Sep 30, 2021

Setting the remote URL with the following command above didn't work for me:

git remote set-url --add --push origin

I tried the removing the --add parameter and only doing the push, and it also didn't work. (I didn't think to swap the two parameters like another poster above mentioned). Anyway, I went directly to the config file and added the URL manually there:

  1. In Windows File Explorer, navigate to the project folder (ensure the File Explorer Hidden items checkbox is selected)
  2. Navigate to .git folder > config file and open with an editor
  3. Under [remote "origin"], add "url = [path of your remote repo, minus the brackets]"
  4. Save and close the file
  5. Make a change to the code, and navigate to both remote repositories to ensure the change "took" in both places (in my case, GitHub and Azure DevOps)

This worked for me, hope it helps others!

Copy link

Remzi1993 commented Oct 31, 2021

Ops. I just realized that I have the gitlab access token stored locally but github requires a password. So now when I push, it only pushes to gitlab. What should I do in this case?

Use SSH for both of them.

Use ssh-keygen to create a key:
ssh-keygen -t rsa -C

The next step is to print out your newly generated SSH key by running the command below:
cat ~/.ssh/

Next, copy your ssh key (starting with ssh and ending at your email address) and paste it in your GitHub, Gitlab and whatever account

Also add you key to the ssh agent
ssh-add ~/.ssh/id_rsa

Once you've done this, you can check and see if it worked:
ssh -T

Copy link

Is there a way to pass your ssh password to the command so that it doesn't have to be input twice?

Copy link

@AntumDeluge just use SSH Agent: ssh-add

Copy link

@AntumDeluge just use SSH Agent: ssh-add

@renepardon This is helpful. But, I don't want to store my password for the entire session. I still want to input it when I execute the command, I just don't want to have to input it twice.

Copy link

Just what I needed, thank you!

Copy link

What if I don`t want always to push to both repos?

Copy link


Copy link

Awesome thanks 🔥

Copy link

Simple to work, tks!

Copy link

After pushing to multiple repositories at a same time, I am getting this error while raising the pull request.
"[There isn't anything to compare. Nothing to compare, branches are entirely different commit histories]".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment