The OP asks:
I learned that I can make it work with:
git branch --set-upstream my_branch origin/my_branch
But why do I need to do this for every branch I create?
You do not need to set upstream all the time.
Not anymore (eleven years later).
With Git 2.37 (Q3 2022), a git config --global push.autoSetupRemote true will take care of that for you.
See commit 05d5775, commit 8a649be, commit bdaf1df (29 Apr 2022) by Tao Klerks (TaoK).
(Merged by Junio C Hamano -- gitster -- in commit f49c478, 26 May 2022)
push: new config option "push.autoSetupRemote" supports "simple" push
Signed-off-by: Tao Klerks
In some "simple" centralized workflows, users expect remote tracking branch names to match local branch names.
"git push"(man) pushes to the remote version/instance of the branch, and "git pull"(man) pulls any changes to the remote branch (changes made by the same user in another place, or by other users).
This expectation is supported by the push.default default option "simple" which refuses a default push for a mismatching tracking branch name, and by the new branch.autosetupmerge option, "simple", which only sets up remote tracking for same-name remote branches.
When a new branch has been created by the user and has not yet been pushed (and push.default is not set to "current"), the user is prompted with a "The current branch %s has no upstream branch" error, and instructions on how to push and add tracking.
This error is helpful in that following the advice once per branch "resolves" the issue for that branch forever, but inconvenient in that for the "simple" centralized workflow, this is always the right thing to do, so it would be better to just do it.
Support this workflow with a new config setting, push.autoSetupRemote, which will cause a default push, when there is no remote tracking branch configured, to push to the same-name on the remote and --set-upstream.
Also add a hint offering this new option when the "The current branch %s has no upstream branch" error is encountered, and add corresponding tests.
The new hint is:
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'
git config now includes in its man page:
push.autoSetupRemote
If set to "true" assume --set-upstream on default push when no
upstream tracking exists for the current branch;
This option takes effect with push.default options 'simple', 'upstream', and 'current'.
It is useful if by default you want new branches to be pushed to the default remote (like the behavior of 'push.default=current') and you also want the upstream tracking to be set.
Workflows most likely to benefit from this option are 'simple' central workflows where all branches are expected to have the same name on the remote.