2

I have several remotes with different push configurations; e.g.

[remote "public"]
    url = ssh://external-server/repo
    push = refs/heads/master:refs/heads/master

[remote "internal"]
    url = ssh://internal-server/repo
    push = refs/heads/*:refs/heads/*

Is it possible to push to both repositories with a single, simple command without inventing additional layers?

I tried pushUrl as suggested in related questions but this expects an url and does not allow a remote. Then, I modified it to use git-remote-ext like in

[remote "all"]
    pushUrl = "ext::git push public"
    pushUrl = "ext::git push internal"

But this pushes to the first repository only and fails then with

fatal: Could not read from remote repository.

Manual shell commands (git push public && git push internal) are not a solution because in reality I have a lot of repositories which are having different sets of remotes (e.g. git servers hosted by customers, public repositories, internal/external mirrors, test systems, read-only sources). Sometimes they are in submodules which I want to push in a way like

git submodule foreach 'git push all'

Defining an alias might work but is too clumsy and breaks somehow the usual workflow (using git push to push things).

ensc
  • 6,704
  • 14
  • 22

1 Answers1

1

The related question should be "Git - Pushing code to two remotes", and it involves a remote 'all' with push urls.

ext::git push public is not an url. ssh://external-server/repo is.

git remote rm all 
git remote add all ssh://internal-server/repo
git remote set-url --add --push all ssh://internal-server/repo
git remote set-url --add --push all ssh://external-server/repo

By default, it will use the push refspec refs/heads/*:refs/heads/*

If you set your default push policy to upstream, only branches which have an upstream branch will be pushed.

git config push.default simple

~

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • how do you specify the different `push` setups here? E.g. I want every branch on the internal server but only `master` on the external one. – ensc Jul 22 '16 at 12:23
  • @ensc not sure. I would go with the push.policy (answer edited), but you need to test it out and see what branch ends up being pushed (using dummy remotes of course, not the actual internal and external remote: just for testing) – VonC Jul 22 '16 at 13:08
  • hacky.... how would you express forced pushes for `-next` branches to the internal repository (`push = +refs/heads/next:refs/heads/next'`)? – ensc Jul 22 '16 at 13:12
  • Sometimes, my local and remote branch names differ; e.g. when mirroring a source repository, I write `push = refs/remotes/foo/*:refs/heads/*`. The push policy will not work in this case. – ensc Jul 22 '16 at 13:14
  • Sure, let me know how your tests turn out – VonC Jul 22 '16 at 13:15