I create a new branch in Git:
git branch my_branch
Push it:
git push origin my_branch
Now say someone made some changes on the server and I want to pull from origin/my_branch
. I do:
git pull
But I get:
You asked me to pull without telling me which branch you
want to merge with, and 'branch.my_branch.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.
If you often merge with the same branch, you may want to
use something like the following in your configuration file:
[branch "my_branch"]
remote = <nickname>
merge = <remote-ref>
[remote "<nickname>"]
url = <url>
fetch = <refspec>
See git-config(1) for details.
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? Isn't it obvious that if I push my_branch
into origin/my_branch
, then I would want to pull origin/my_branch
into my_branch
? How can I make this the default behavior?
branch.autosetupmerge
means that the upstream configuration for a new branch is only automatically set when creating a branch from a remote-tracking branch (e.g.<remote-name>/<branch-name>
) (see git-config(1)). You are probably creating your branches from existing local branches. If you are effectively branching directly from the tip of a remote branch (despite being on a local branch), then you can usegit branch my_branch <remote-name>/<branch-name>
to automatically setup the upstream configuration. – Commissariat--set-upstream
option is deprecated. You should use--track
or--set-upstream-to
instead. – Gallimaufry--set-upstream
is deprecated, then perhaps the git devs should remove it from the help message that gets displayed when you rungit push
with no options and no upstream is set? – JokjakartaBRANCH=$(git symbolic-ref --short HEAD) && git branch --set-upstream-to=origin/$BRANCH $BRANCH
– Curchgit pull
rather thangit push
, right? – Cartagegit push
. Specifically the modern usage to set the upstream branch to which you want to push, isgit push -u origin branchname
, as described in the answer by @Mark Longair. Withgit pull
, the-u
does a different thing, and I find that I would rather specify each time what branch to pull from anyway. – Jokjakartagit branch --set-upstream
is deprecated.git push --set-upstream
is not. – Corroboration--set-upstream
option.branch --set-upstream
has been deprecated in Git 1.8 and had finally been retired in Git 2.15 on Nov 2017. – Barmaidgit branch --set-upstream-to
? Or perhapsgit push --set-upstream
? I'm not sure ... but my installed Git 2.25 doesn't seem to be aware that this has been retired 😁 – Myrickupstream
related toforking
in git, but this context is not meant here. So I also wonder whyupstream
is needed here. – Beatergit config --global push.autoSetupRemote
is enough! – Digitigrade