Search code examples
gitgit-remote

How to setup current branch (dev) to push to refs/remotes/test with git push only?


I have setup my current branch dev to push to refs/remotes/test with the following git push --set-upstream origin dev:test.

.git/config looks like:

[remote "origin"]
    url = https://[email protected]/appulocal/test_st.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "dev"]
    remote = origin
    merge = refs/heads/test

but when I do git push it does not push from local dev to test on remote. Git bash shows the message:

fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:test

To push to the branch of the same name on the remote, use

    git push origin dev

To choose either option permanently, see push.default in 'git help config'.

How to resolve this so that only on doing git push my current branch dev pushes to test on remote?


Solution

  • Actually you have set up dev to pull from refs/remotes/test. That's what the branch's merge option governs. It is a bit confusing, since this was set using a push command; but a different set of config options governs how push behaves.

    The thing is, git checks a number of things to decide what to do with a push, and the behavior most of us are used to is a default for when none of them are set... but git isn't willing to rely on that default if branch names don't match, because it sees this as ambiguous between two different configurations you might be trying to use.

    In your case, you want to specify a push destination of test for your dev branch. There are a few ways to address this, but I can't think of any that don't have some side-effect when you're on a different branch and say git push.

    Assuming you only push to one remote - and its the same one you pull from - you could set the push.default config value

    git config push.default upstream
    

    This removes the "same name" check. (Note that it removes this check for all branches; but that's ok as long as you intend to always push to whatever branch you merge from during a pull.) The official docs consider this reasonable only for single-remote workflows.

    If you want more precision in how you specify what pushes to where, you may just need to specify refspecs on the push command line.