Skip to content

Instantly share code, notes, and snippets.

@rvl
Created February 9, 2016 11:41
Show Gist options
  • Save rvl/c3f156e117e22a25f242 to your computer and use it in GitHub Desktop.
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.

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

@mubaraqwahab
Copy link

Thank you very much!

@Codesait
Copy link

very nice

@MustafaAnasKH99
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?

@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

thanks a lots

@tewshi
Copy link

tewshi commented Jul 13, 2020

awesome...

@shovradas
Copy link

cool

@AltusJVR
Copy link

That is awesome, thank you so much!

@glureau-betclic
Copy link

Super useful, big thanks!

@gremlin-master
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

@gregsadetsky
Copy link

Thank you!

@nomad-web
Copy link

Thanks!

@iAluucard
Copy link

Thanks this is gold.

@devinou971
Copy link

Thanks Really cool 👍

@AsifulNobel
Copy link

AsifulNobel commented Jul 10, 2021

Thanks a lot for making this 👋

@cratermoon
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?

@carloswm85
Copy link

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

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

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

@AntumDeluge just use SSH Agent: ssh-add

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

@AllanT102
Copy link

Just what I needed, thank you!

@IgorStanivukovic97
Copy link

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

@ankit-halfsquare
Copy link

OSM

@Florencemartino
Copy link

Awesome thanks 🔥

@DejavuMoe
Copy link

Simple to work, tks!

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