Chris Stryczynski
Chris Stryczynski

Reputation: 33881

Is it possible to push a branch to two remotes, each with different branch names?

I have two repositories (two remotes).

I have a branch staging that I would to push to remote a on branch staging and remote b on branch master.

So essentially:

staging -> a/master
staging -> b/staging

Is this possible to configure with git's config? So I would need a single git push?

As an alternative I can do:

git push a staging/master
git push b staging/staging

Upvotes: 0

Views: 37

Answers (2)

shender
shender

Reputation: 1283

Not technically with a literal single git push, but you can achieve effectively the exact desired behavior with a local (project only, aka not --global) git alias.

Edit the file .git/config in your project directory. It's best to edit the file directly rather than running git config ... from a terminal, to avoid quotation/escape issues. Add this to the bottom:

[alias]
  pushall = "!git push a staging:master; git push b staging"

(pushall can be whatever you want, as long as it's not a default git command.)

The ! means run it from a shell (like typing it into terminal). This command says "push local staging branch to master on remote a, and push local staging branch staging on remote b, only.

To test, we have two remotes with a master branch and staging branch:

I] sean at goz in ~/d/stack (staging|✔)
> git remote -v
a       [email protected]:sh78/stack.git (fetch)
a       [email protected]:sh78/stack.git (push)
b       [email protected]:musophob/stack.git (fetch)
b       [email protected]:musophob/stack.git (push)

Now we make some changes and commit:

[I] sean at goz in ~/d/stack (staging|✔)
> touch afile
[I] sean at goz in ~/d/stack (staging|…1)
💥 (0) git add afile
[I] sean at goz in ~/d/stack (staging|●1)
> git commit -m "test"
[staging 64e5ee6] test
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 afile

Now we use our local alias:

[I] sean at goz in ~/d/stack (staging↑1|✔)
> git pushall
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 269 bytes | 89.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:sh78/stack.git
  bb8587b..64e5ee6  staging -> master
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 269 bytes | 269.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote:
remote: Create pull request for staging:
remote:   https://bitbucket.org/musophob/stack/pull-requests/new?source=staging&t=1
remote:
To bitbucket.org:musophob/stack.git
  1e5a81a..64e5ee6  staging -> staging

The result is that our commit with afile is pushed to remote a's master branch (not staging) and simultaneously pushed to remote b's staging branch (not master). I confirmed this with live remotes using GitHub (™ Microsoft Corporation) and BitBucket.

Upvotes: 1

phd
phd

Reputation: 94423

No, you cannot configure 2 remotes for a branch and cannot configure push.default to push to differently named branches. You need to run these commands:

git push a staging:master
git push b staging:staging

The last one can be abbreviated as

git push b staging

You can configure the upstream remote for the branch with the command

git push -u b staging

and now bare git push will push staging to remote b branch staging, but that's all. The first command (git push a staging:master) cannot be shortened or automated.

Upvotes: 1

Related Questions