Search code examples
gitgit-branchgit-remote

Change the default branch for each configured Git Remote


I know you can change the default remote for a branch by doing:

git push -u remote branch

But I have a few remotes, and I want to just be able to do:

git pull remote1 git push remote2

And have a default branch for each remote (probably the same, but not necessarily).

I was hoping to avoid doing git pull remote branch and git push remote branch each time.

My current config has this:

[branch "main"]
    remote = origin

And that is just from doing the aforementioned git push -u ... for my 'primary' remote.


Solution

  • Three-way ("triangular") work-flows are not all that well supported in Git, but for certain special cases, git config push.default can be set usefully, and in other special cases, remote.remote.pushurl can be set usefully.

    The problem at the most fundamental level is that any one branch can only have one upstream set; the upstream combines both the name of the remote and the name of the branch as found on that remote, so if branch B has upstream U set, running git push with no arguments pushes to the (single) assigned remote, using the (single) assigned name, and git pull fetches from the (same, single) assigned remote and merges from the (same) single assigned name.

    Using git push -u is just a short way to say: after my git push succeeds, please run git branch --set-upstream-to for me as well. Since that sets the (one, single) upstream, it's almost certainly not what you want to do here: either the upstream is already set correctly, or you want to set it before (not after) the git push command.

    Your best best here is probably to stop using git push and git pull. Instead of those commands, write your own command(s), that use your own configuration setup(s) to do whatever it is you want done. Run those commands, not git push and git pull. (Those commands will then eventually run Git commands, maybe even git push and git pull, but with appropriate remote names and refspecs.)

    Failing that, look into the settings available for push.default, push.defaultRemote, and remote.name.push, as well as the above-mentioned remote.remote.pushurl settings, all described in the git config documentation.