-1

In the config file, a branch is set up to have an upstream in a remote repository

[branch "mybranch"]
    remote = myremote
    merge = refs/heads/mybranch

Can we set up an upstream in a third remote repository, so that when the current branch is mybranch, git push <remote> will work without refspec specified for the branches? For example, will the following work?

[branch "mybranch"]
    remote = myremote
    merge = refs/heads/mybranch

[branch "mybranch"]
    remote = yourremote
    merge = refs/heads/mybranch
halfer
  • 19,824
  • 17
  • 99
  • 186
Tim
  • 1
  • 141
  • 372
  • 590
  • Possible duplicate of [Default behavior of "git push" without a branch specified](https://stackoverflow.com/questions/948354/default-behavior-of-git-push-without-a-branch-specified) – phd Jan 12 '19 at 19:37
  • https://stackoverflow.com/search?q=%5Bgit-push%5D+without+branch – phd Jan 12 '19 at 19:37
  • What do you mean by *set up an upstream **in a second remote repository**?* The branch named `mybranch` can only have one remote, but each repository has its own set of branches, so the only way I can make sense of this is that you're asking: *If I make a different clone, that has a different set of branches, can I set an upstream for one of those branches?* and of course the answer to that is obvious. – torek Jan 12 '19 at 19:51
  • Thanks. Updated @torek – Tim Jan 12 '19 at 20:12

1 Answers1

1

For example, will the following work?

[branch "mybranch"]
    remote = myremote
    merge = refs/heads/mybranch

[branch "mybranch"]
    remote = yourremote
    merge = refs/heads/mybranch

No. Git configuration files are flexible enough to support this, though there's no need for a second section. That is, you can write this more simply as:

[branch "mybranch"]
    remote = myremote
    remote = yourremote
    merge = refs/heads/mybranch

but only a few parts of Git make any use of multiple settings.1 The git push command in particular decides whether you've specified a remote, and if you have not, it gets the last setting for branch.branch.remote (where branch is the current branch). Then it decides whether you've specified a refspec, and if not, uses your push.default setting to decide how to ask the other Git to update one of its names based on your branch's current commit. So:

  • if you specify a remote argument, the two remote settings are irrelevant;
  • if not, Git takes the last one.

This means that all settings except for the last become irrelevant.

(There are ways to use triangular workflows, where you fetch from one repository but push to another, but this is not one. Use google search to find some.)


1The git fetch command is one that does make use of multiple settings: it takes all of the fetch = ... settings for any given remote, when it is doing a defaulted git fetch. That is:

[remote "origin"]
    fetch = +refs/heads/master:refs/remotes/origin/master
    fetch = +refs/heads/develop:refs/remotes/origin/develop

produces a repository that acts a lot like like a single-branch clone, except that it's what we might call a two-branch clone.

torek
  • 448,244
  • 59
  • 642
  • 775