0

tl;dr: On git push I would like to achieve the following:

local/staging -> wpengine-staging/master
local/staging -> gitlab/staging

local/master -> wpengine-production/master
local/master -> gitlab/master

Details: I am developing a site on https://wpengine.co.uk/ using their staging and production environments as well as their git push feature.

Wpengine has production on a git repo/branch production/master and staging on a repo/branch staging/master.

To allow me to push to these respectively from one local repo with staging/master branches I have set up two remotes: wpengine-production and wpengine-staging.

I then set the upstream for my local staging to wpengine-staging master and for my local master to wpengine-production master as well as git config push.default upstream

So far so good - now when I git push from either branch it sends my commits to the desired remote and branch.

Next I need to keep a copy of the code on a gitlab repo.

For this my local repo should should push staging to gitlab/staging and master to gitlab/master.

Now, I am aware of Git - Pushing code to two remotes and that should work just fine for the master branch.

I am however, struggling with the setup for the staging branch since, if I used two push URLs on a single remote, I would need to set two different upstreams on the same branch for each URL/branch.

As far as I can tell this isn't possible.

So setup wise I could have either:

Two remotes with two push URLs each, let's call them production and staging:

production -> gitlab & wpengine-production

staging -> gitlab & wpengine-staging

Or, I could have three remotes, one for each URL:

gitlab, wpengine-staging, wpengine-production.

I know I could set an alias to push to two remote branches and that would be fine if it was just me but since others will be working on the project I really want to make this as fool proof as possible and enable this behaviour on git push.

As a side note - on git pull I need it to take from the wp-engine remotes but that part I don't have an issue with (I think).

Finally, I am well aware that what I am trying to achieve probably isn't possible, and for good reasons (remotes should be explicitly stated on push / pull on complex setups like this). The only reason I am exploring this is to avoid human error when working with this down the line.

Bananaapple
  • 2,984
  • 2
  • 25
  • 38
  • 1
    Do you always want the 2 servers to be in sync (i.e never push to one, but not the other?) If so, then why not set up hooks on one of the servers to mirror all pushes to the other one? – match Jan 19 '18 at 11:56
  • Does this answer your question? [Git - Pushing code to two remotes](https://stackoverflow.com/questions/14290113/git-pushing-code-to-two-remotes) – Tom Hale Jun 30 '23 at 14:16

1 Answers1

1

I think this is what you need:

Git - Pushing code to two remotes

So basically you set up multile push url for the same remote, one for gitlab, one for the other, keeping all remotes synced.

Or you can create server side hooks, so for example gitlab will push to the other one.

MrKekson
  • 720
  • 6
  • 18
  • 1
    I reference that answer in my question, pointing out that it does not solve the issue of "if I used two push URLs on a single remote, I would need to set two different upstreams on the same branch for each URL/branch" - is there a way around that? That said though, the hook on gitlab is a good idea. I'll see if that does the trick. – Bananaapple Jan 19 '18 at 12:08
  • 1
    Oh, sorry, you are right about the multiple upstream. But you can set your local git behavior, so if you push without any arguments, then it will default to origin with your current branch. In this way you don't have to set up upstreams for all the branches, what is handy if you have hundred of branches anyways :D – MrKekson Jan 22 '18 at 14:37