gitgithubbranchupstream-branch

How to set a custom upstream for a branch during its creation?


I have a workflow in which local branches end up with names that I don't really want to be visible in the remote repository. I would like to set the upstream name when I create the local branch, so I don't need to remember to do it when I use git push.

I've looked at a lot of questions/answers here, and I've seen two recommendations for setting upstream branch names:

  1. Use git config --local remote.origin.push LOCAL_BRANCH:REMOTE_BRANCH
  2. Use git branch --set-upstream-to REMOTE_BRANCH
  3. Use git push -u or git push origin LOCAL_BRANCH:REMOTE_BRANCH

The first one "sort of works" in that if I push the local branch, it wants to push to the expected remote, but it also resets any existing value in that config entry, which I don't want.

The second doesn't work when the remote branch doesn't exist, giving me:

fatal: the requested upstream branch 'foobar' does not exist
hint: 
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint: 
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.
hint: Disable this message with "git config advice.setUpstreamFailure false"

And the third is obviously not what I'm after, because I want this to occur at branch creation time.

I've tried pushing to a new (differently named) remote and set that the following entries appear in the local config:

branch.foo.remote=origin
branch.foo.upstream=foobar
branch.foo.merge=refs/heads/foobar

But, if I remove them, and recreate them manually with a different name, when I push it (without -u) it just wants to use the local branch name.

So I'm beginning to think that there's not actually a good way to do this.

Just to reiterate, what I want is:

A way to configure a new local branch to have a different remote branch name when it is first pushed, without having to explicitly set that name when it is first pushed

Is this possible?

EDIT: It turns out that while the given answer works for command line git, it does not seem to work in IntelliJ (perhaps IntelliJ caches git config information?).


Solution

  • A way to configure a new local branch to have a different remote branch name when it is first pushed, without having to explicitly set that name when it is first pushed

    Before its first push is easy, you can set the upstream branch name any time. Bypass the safety checks by using the core commands, the conveniences are checking for likely typos for you and such, you're just taking care of business yourself. So, using your example from the question

    git config push.default upstream
    git config branch.test.remote origin
    git config branch.test.merge refs/heads/my-test
    

    gives test's upstream and its branch name there without checking that either have been set up yet.

    p.s. see the push.default docs: Git ordinarily defaults to branches with the local name, to get it to use a different, configured name by default you also have to ask for upstream as the default.