Git: make existing branch track remote branch

If you have an existing branch, say master, that was not cloned from a remote repository, a git pull without further arguments will give you the following error message:

You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.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 ').
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 "master"]
remote =
merge =

[remote ""]
url =
fetch =

See git-config(1) for details.

Editing .git/config by hand is cumbersome, and I never can remember what the “syntax” for is.

As of git 1.7.0, it is now really easy to set this up:

git branch --set-upstream master origin/master

which will put the following into your .git/config

[branch "master"]
remote = origin
merge = refs/heads/master

Now you can git pull and the remote changes in origin/master will be automatically merged into your local master branch.