Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.

Remotes

Suppose your git remotes are set up like this:

git remote add github git@github.com:muccg/my-project.git
git remote add bb git@bitbucket.org:ccgmurdoch/my-project.git

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@github.com:muccg/my-project.git
git remote set-url --add --push origin git@bitbucket.org:ccgmurdoch/my-project.git

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

Per-branch

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

References

@samirillion
Copy link

samirillion commented Aug 29, 2019

noice

@fredericpfisterer
Copy link

fredericpfisterer commented Sep 3, 2019

Very cool !

@primelos
Copy link

primelos commented Dec 9, 2019

Boom! thank you!

@renepardon
Copy link

renepardon commented Dec 10, 2019

git remote set-url --add --push origin <...>

It's that simple 👍

@Skura23
Copy link

Skura23 commented Jan 14, 2020

Thanks, very helpful.

@mubaraqwahab
Copy link

mubaraqwahab commented May 11, 2020

Thank you very much!

@Codesait
Copy link

Codesait commented May 26, 2020

very nice

@MustafaAnasKH99
Copy link

MustafaAnasKH99 commented Jun 7, 2020

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?

@GretaP
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

@blackinitial
Copy link

blackinitial commented Jul 2, 2020

thanks a lots

@tewshi
Copy link

tewshi commented Jul 13, 2020

awesome...

@shovradas
Copy link

shovradas commented Jul 13, 2020

cool

@AltusJVR
Copy link

AltusJVR commented Sep 25, 2020

That is awesome, thank you so much!

@glureau-betclic
Copy link

glureau-betclic commented Sep 28, 2020

Super useful, big thanks!

@gremlin-master
Copy link

gremlin-master commented Oct 21, 2020

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

@gregsadetsky
Copy link

gregsadetsky commented Jan 9, 2021

Thank you!

@nomad-web
Copy link

nomad-web commented Feb 16, 2021

Thanks!

@iAluucard
Copy link

iAluucard commented Feb 19, 2021

Thanks this is gold.

@devinou971
Copy link

devinou971 commented Apr 6, 2021

Thanks Really cool 👍

@AsifulNobel
Copy link

AsifulNobel commented Jul 10, 2021

Thanks a lot for making this 👋

@cratermoon
Copy link

cratermoon commented Jul 30, 2021

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?

@carloswm85
Copy link

carloswm85 commented Aug 3, 2021

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

@malantin
Copy link

malantin commented Aug 4, 2021

Very helpful. Thank you!

@prmichaelsen
Copy link

prmichaelsen commented Sep 5, 2021

neat 📸

@rhc822
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 git@github.com:muccg/my-project.git

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!

@Remzi1993
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 YOUR@EMAIL.com

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

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 git@github.com

@AntumDeluge
Copy link

AntumDeluge commented Feb 16, 2022

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

@renepardon
Copy link

renepardon commented Feb 16, 2022

@AntumDeluge just use SSH Agent: ssh-add

@AntumDeluge
Copy link

AntumDeluge commented Feb 25, 2022

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

@AllanT102
Copy link

AllanT102 commented Feb 28, 2022

Just what I needed, thank you!

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